I'm trying to get location manager to work in an Activity in my Android app but it keeps giving me the error "cannot resolve method requestLocationUpdates"
I'm putting this code in my OnCreate (found from other examples):
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
I want to use my own "OnLocationChanged" method to update a map with the users location, but I can't figure out why I'm getting this error.
Any ideas why I'm getting the error?
Is there a better more modern way to implement location updates?
Thanks!
I would recommend you look into the new FusedLocationProviderApi
that is part of Google Play Services. From the docs:
The Google Location Services API, part of Google Play Services, provides a more powerful, high-level framework that automatically handles location providers, user movement, and location accuracy. It also handles location update scheduling based on power consumption parameters you provide. In most cases, you'll get better battery performance, as well as more appropriate accuracy, by using the Location Services API.