Sunday, 27 January 2013

Where Am I example in Android


main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="0Ghwgs7BRSsKfxNMci8ovGa24E2irA0ddgwWzCg"
        android:clickable="true" />

</RelativeLayout>

LocationOverlay.java:
package com.whereami.inspire.ram;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class LocationOverlay extends ItemizedOverlay<OverlayItem> {
    private List<GeoPoint> mapItems;

    public LocationOverlay(Drawable marker) {
        super(boundCenterBottom(marker));
    }

    public void setItems(ArrayList<GeoPoint> items) {
        mapItems = items;
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return new OverlayItem(mapItems.get(i), null, null);
    }

    @Override
    public int size() {
        return mapItems.size();
    }

    @Override
    protected boolean onTap(int i) {
        // Handle a tap event here
        return true;
    }
}

WhereAmIActivity.java:
package com.whereami.inspire.ram;

import java.util.ArrayList;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class WhereAmIActivity extends MapActivity {
MapView map;
MapController mapController;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
map = (MapView) findViewById(R.id.mapview);
mapController = map.getController();

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Location location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

double lat = location.getLatitude();
double longi = location.getLongitude();

ArrayList<GeoPoint> locations = new ArrayList<GeoPoint>();

locations.add(new GeoPoint((int) (lat * 1E6), (int) (longi * 1E6)));

LocationOverlay myOverlay = new LocationOverlay(getResources().getDrawable(R.drawable.baloon));
myOverlay.setItems(locations);
map.getOverlays().add(myOverlay);
mapController.setZoom(15);
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.whereami.inspire.ram"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".WhereAmIActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <uses-library android:name="com.google.android.maps" />
    </application>

</manifest>




No comments:

Post a Comment