Unable To Get User Location In Android
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()
ifgetLastKnownLocation()
either returnsnull
or aLocation
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"