Skip to content Skip to sidebar Skip to footer

Map Location Returning Null For Android

My getLastKnownLocation is turning up 'null'. I can get the locations I put in to show however get the users location has been trying. From the docs, here, and 4 05 tutorials I t

Solution 1:

Instead of getLastKnownLocation, try requestLocationUpdates. You can use a LocationListener to listen for location updates. More info: https://developer.android.com/reference/android/location/LocationListener.html

Solution 2:

getLastKnownLocation gives nulls, a lot, it's just the nature of the beast.

Use a LocationListener and wait if you want to secure a solid position. getLastKnownLocation is really only good for priming location based logic being feed by a LocationListener.

Solution 3:

Generally speaking whats going on is if you request a location for a detector (like GPS) and it has never gotten a location since its last boot up, it will return null. This is why it is usually better to request the updates first, which will cause the device to actively find one.

One technique is to find the location BEFORE you need it, then use getLastKnownLocation when you actually need it.

When using getLastKnownLocation it is always best to a) do your best to ensure that the location tracking device your using has already found a location as some point during this boot, and b) you check for nulls just in case.

As a side note, I would recommend checking the accuracy when getting new locations. It is very common that the first location found is not that as accurate as it could be, at the same time if you look for a period of time the LAST location found may not be as accurate as some other location found during the cycle. The key is use the GPS as little and as well controlled as possible, and double check your results.

Post a Comment for "Map Location Returning Null For Android"