Mega Code Archive

 
Categories / Android / Hardware
 

Custom Location Overlay

package app.test; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.MyLocationOverlay; import android.os.Bundle; import android.content.Context; import android.location.Location; import android.util.Log; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapView; import com.google.android.maps.MyLocationOverlay;  class MyCustomLocationOverlay extends MyLocationOverlay {     MapView mMapView = null;        public MyCustomLocationOverlay(Context ctx, MapView mapView) {     super(ctx, mapView);     mMapView = mapView;   }   public void onLocationChanged(Location loc) {     super.onLocationChanged(loc);     GeoPoint newPt = new GeoPoint((int) (loc.getLatitude()*1E6),         (int) (loc.getLongitude()*1E6));     Log.v("MyCustomLocationOverlay", "Got new location: " + newPt);     mMapView.getController().animateTo(newPt);   } } public class MyLocationDemoActivity extends MapActivity {          MapView mapView = null;     MapController mapController = null;     MyLocationOverlay whereAmI = null;     @Override     protected boolean isLocationDisplayed() {         return whereAmI.isMyLocationEnabled();     }     @Override     protected boolean isRouteDisplayed() {         return false;     }     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         mapView = (MapView)findViewById(R.id.geoMap);         mapView.setBuiltInZoomControls(true);                  mapController = mapView.getController();         mapController.setZoom(15);         whereAmI = new MyCustomLocationOverlay(this, mapView);     mapView.getOverlays().add(whereAmI);     mapView.postInvalidate();     }     @Override     public void onResume()     {       super.onResume();         whereAmI.enableMyLocation();     whereAmI.runOnFirstFix(new Runnable() {             public void run() {                 mapController.setCenter(whereAmI.getMyLocation());             }         });     }     @Override     public void onPause()     {       super.onPause();         whereAmI.disableMyLocation();     } } //main.xml <?xml version="1.0" encoding="utf-8"?> <!-- This file is /res/layout/main.xml --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="fill_parent"         android:layout_height="fill_parent">     <com.google.android.maps.MapView         android:id="@+id/geoMap" android:clickable="true"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:apiKey="yourMapKey"         /> </RelativeLayout>