Search code examples
androidgoogle-mapsgpsgoogle-maps-api-2

Gps tracking using google direction api in android


In my application to set a driving path then start GPS track. the moving location color as red. I am using google map direction api. To set a direction path is working fine. But gps tracking doesn't work.

How to enabled gps and gps tracking path color?

Here is my code:

public class MainActivity extends MapActivity implements LocationListener {

    private static final String Location = null;
    public MapView mapView ;
    public List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
    private LocationManager lm;
    private LogOverlay logOverlay;
    private GpsLog log;
    double lat;
    double lon;
    public GeoPoint  srcGeoPoint,destGeoPoint,interGeoPoint;

    public void onCreate(Bundle savedInstanceState) {           
    super.onCreate(savedInstanceState);   
    System.out.println("*************1**************1");
    setContentView(R.layout.activity_main);
    System.out.println("*************2**************");
    mapView = (MapView) findViewById(R.id.mapv); 
    mapView.setBuiltInZoomControls(true);
    System.out.println("*************3**************");
    lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 10,this);                           
    double src_lat = 12.98472; // the testing source 
    double src_long = 80.17778; 
    double dest_lat = 13.00833; // the testing destination 
    double dest_long =80.2125;         
    double via_lat = 13.065; // the testing destination 
    double via_long = 80.2325;
    srcGeoPoint = new GeoPoint((int) (src_lat * 1E6), 
    (int) (src_long * 1E6)); 
    destGeoPoint = new GeoPoint((int) (dest_lat * 1E6), 
    (int) (dest_long * 1E6)); 

    interGeoPoint = new GeoPoint((int) ( via_lat * 1E6), 
          (int) (via_long * 1E6));               
    mapView.getController().animateTo(srcGeoPoint);
    mapView.getController().setZoom(15);
    new LongOperation().execute("");       
    initMyLocation();  
    final MyLocationOverlay overlay = new MyLocationOverlay(this, mapView);
    overlay.enableMyLocation();
    overlay.enableCompass();
    mapView.invalidate();
    System.out.println("*************4**************");
}

private void initMyLocation() {
    System.out.println("init my location *************");
    // does not work in emulator                 
}

Solution

  • Hi Add check this missing code snippet

    // Adding current location on map view
    
    private void addMyLocationOverlay() {
        myLocation = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(myLocation);
    
        myLocation.enableMyLocation();
    
        myLocation.runOnFirstFix(new Runnable() {
            @Override
            public void run() {
                mapController.animateTo(myLocation.getMyLocation());
            }
        });
    
    }
    

    This code will show current location animated blue white blinking dot on mapview

    No Need to use location manager & calling requestLocationUpdates