Custom Location Overlay : Location « Hardware « Android






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>

   
  








Related examples in the same category

1.Location service and LocationManager
2.Location service
3.Using LocationManager
4.My location
5.Display GEO location
6.Using location service for the weather
7.Using Intent to go to a geo location
8.Location based service
9.My location and Google Map
10.Get my location
11.Geo location and Google Map
12.Location Tracking
13.A light pool of objects that can be resused to avoid allocation.
14.extends android.location.Location
15.Geo Location Util
16.C:\Java_Dev\WEB\dev\android\weatherforecastsystem-read-only\com\hci\pwf\LocationUtil.java
17.upload Data with Geo location
18.Copy a file from one location to another.
19.LocationManager.GPS_PROVIDER
20.Location util