0

I am working on an on demand location providing solution but I find that the documentation are too confusing for me to find the most efficient solution.

Here's what I understood so far:

LocationManager.getLastKnownLocation() is basically a piggybacking method. If no other app has location enabled, your app will not return a location.

LocationManager.requestLocationUpdates(), I understand that this basically sets up a listener for location updates. The actual method that's called is "OnLocationChanged()" of the listener. So technically you can put your location display code in the "onLocationChanged"...

If that's the case... How do I actually get the updated location? I don't want to be updated EVERYTIME the location changed. I want the location on demand, not whenever it updates. How would I implement that?

===

My current guess is that... Once you setup the listener, whenever you call "getLastKnownLocation", it will be an updated one created by your listener. Is that true? Is that how it works?

Ying Li
  • 2,500
  • 2
  • 13
  • 37
  • Possible duplicate of [getLastKnownLocation vs requestLocationUpdates](http://stackoverflow.com/questions/22575433/getlastknownlocation-vs-requestlocationupdates) – Farhad Feb 19 '16 at 20:20
  • please have a look at this answer: http://stackoverflow.com/a/33599343/4032259 – Max Feb 19 '16 at 20:21

3 Answers3

1

If you turn off the location on the phone, and then turn it on, the last know location will return null.

Now the actual method to start getting new location is the method

requestLocationUpdates()

This will update the

getLastKnownLocation() 

and while requestLocationUpdates() is being called, the

onLocationChanged(Location location)

will be called to invoke things that you need to do with the location object.

So inside this method you manage the new location/ location changes.

MoGa
  • 635
  • 6
  • 14
0

If you want only one location, and only when you call it, use requestSingleUpdate(). Check out the documentation about it here. Keep in mind that if you use GPS and your inside a building, it might take a little while to return a location update.

Pablo Baxter
  • 2,144
  • 1
  • 17
  • 36
0

If you have the ability to use the GoogleApiClient as shown in the comments above by Max kpd (How do I get the current GPS location programmatically in Android?) that would probably be the best option.

If you don't want to rely on the Google API being available, I would suggest using both options to improve the user experience. The initial call may or may not be null, but also may or may not be from a while ago. Initially, I would call getLastKnownLocation, and if it returns something use it in your app, updating/initializing the interface.

At the same time, request location updates with requestSingleUpdate if you only want 1, or requestLocationUpdates if you want recurring updates, and when onLocationChanged is fired in your listener, update the location you're using in the app as well as anything the in interface that is affected by it.

Community
  • 1
  • 1
frenziedherring
  • 2,225
  • 2
  • 15
  • 23