For my new app, I need to get user location using GPS. To do so, I am using fusedLocationProviderApi.getLastLocation
. Most of the time I am getting null
.
But then I realized that all the APIs are intended to get notified on Location changed. But I want to take current exact location of user (consider user not moving) in the app.
How do I get the Location object so that I don't get null
each time.
Asked
Active
Viewed 1,690 times
1

Soumyaansh
- 8,626
- 7
- 45
- 45
-
Yeah, I agree about the brief idea about getting location. But my point is that for the above mentioned implementation, onLocationChanged would be called on every change in location, Not just get location of standing user. Or I got it completely wrong? – Dec 07 '15 at 09:35
-
can you please update your locationProvider also in question. – Rahul Dec 07 '15 at 09:42
-
`onLocationChanged()` will fire once the location has been resolved. The user doesn't need to physically move. – Mike M. Dec 07 '15 at 09:43
-
Mike M. Thanks for the insight :) – Dec 07 '15 at 17:45
1 Answers
0
You can use this code in OnCreate()
method to get location
// 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);

Raza Ali Poonja
- 1,086
- 8
- 16