4

Why does my location and marker have different locations? Before adding the marker I click on the floating button and when I click that, I call getLatitude and getLongitude from LocationManager. It's similar to when the is map ready and move camera so I think the marker and mylocation must be the same:

enter image description here

This is my code:

private class MyLocationListeners implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        longitude = null;
        latitude = null;

        longitude = String.valueOf(location.getLongitude());
        Log.d(TAG, "onLocationChanged1: " + longitude);

        latitude = String.valueOf(location.getLatitude());
        Log.d(TAG, "onLocationChanged2: " + latitude);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

Code where I set MyLocationListener:

locationListener = new MyLocationListeners();
locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,
        5000,
        10,
        locationListener
);
Daniel
  • 2,355
  • 9
  • 23
  • 30
Eggy
  • 522
  • 5
  • 29

1 Answers1

0

The My location layer uses it's own location provider that is not the Fused Location Provider nor the Location Manager. From the documentation:

A GoogleMap object has a built-in location provider for its my-location layer, but it can be replaced with another one that implements this interface.

Also, the My location layer, the Fused Location provider and the Location Manager can get different locations (it depends on the provider, the GPS and network availability, ...).

You can replace the default location provider for the my-location layer using the GoogleMap.setLocationSource method.

antonio
  • 18,044
  • 4
  • 45
  • 61
  • can you explain detail for me? what must I replace in my code – Eggy Jul 15 '16 at 12:24
  • Here you can find a very googd example that uses fused location provider http://stackoverflow.com/questions/28108326/android-google-maps-location-with-low-battery-usage – antonio Jul 15 '16 at 12:34