juliano.net
juliano.net

Reputation: 8177

Location not being updated automatically

I'm implementing LocationListener in my Fragment class and used the following code to keep track of location updates, although it's not get frequent updates as it should. I need to show users position on map like Google Navigator.

Find the best provider

mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

// if I uncomment the next line, it takes too long to get the location
// criteria.setAccuracy(Criteria.ACCURACY_FINE);
// if I leave the line above commented, the app seems to receiving location from triangulation

mProvider = mLocationManager.getBestProvider(criteria, false);
Location location = mLocationManager.getLastKnowLocation(mProvider);

LocationChanged event

@Override
public void onLocationChanged(Location location) {
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

    setMapCameraPosition(latLng);
}

Upvotes: 0

Views: 82

Answers (1)

Sam
Sam

Reputation: 86958

You are only asking for the last known location, which may or may not exist. In order to use the LocationListener you must call one of the requestLocationUpdates() methods.

mLocationManager.requestLocationUpdates(mProvider, 0, 0, mLocationListener);

Upvotes: 1

Related Questions