Skip to content Skip to sidebar Skip to footer

Unable To Get User Location In Android

Everytime I run the code below, the log spits out 'no location'. I do not know where I am going wrong. I have enabled the location services in the manifest like so :

Solution 1:

getLastKnownLocation() will always return null, unless some app on the device has recently requested location updates using the particular location provider.

Use getLastKnownLocation() either:

  • when you are interested in a location if one happens to be handy, but otherwise can live without it, or

  • as an optimization, where you use requestLocationUpdates() if getLastKnownLocation() either returns null or a Location that you deem to be too old

See the documentation for more guidance on when to use the various methods on LocationManager.

Solution 2:

Because there isn't always a last known location. If you want to ensure you get a location, register for updates and wait for the location to be determined. Network will come quickly, GPS may take a while. Although both have a chance of failing, for example if you aren't connected to wifi/cellular even network will fail, GPS will fail if it can't line of sight enough satellites.

Post a Comment for "Unable To Get User Location In Android"