2

I am trying to use @Fedor's incredibly helpful code from his answer here: What is the simplest and most robust way to get the user's current location on Android?. However I am a little unsure of how to follow his recommendation of making an instance of MyLocation, and using .getLocation(), when it returns a boolean. Does this mean that I will need to, instead of calling MyLocation in a separate class, work in the MyLocation class? Or should I just create another method in the MyLocation class that returns the LocationResult returned in his getLocation() method, if it is true?

**Possible Solution:**Is the locationResult parameter passed in updated so that I can then derive the location from it after the getLocation call?

Thank you very much for your help. If anything is unclear, please let me know.

Community
  • 1
  • 1
NumenorForLife
  • 1,736
  • 8
  • 27
  • 55

1 Answers1

1

You should be using Google Play Services.

Getting the user location is much easier now.

http://developer.android.com/training/location/index.html

dannyroa
  • 5,501
  • 6
  • 41
  • 59
  • Wouldn't I just need to copy all of that code in anyway? I don't see how it's that much simpler? The sample code from the retrieve page is 600 lines – NumenorForLife May 31 '13 at 18:06
  • 1
    You don't need to copy all the code at http://developer.android.com/training/location/retrieve-current.html. I integrated that in my app & it's only 20+ lines. Aside from the code from onCreate, you just need onConnect & onDisconnect. The new Location API is also more accurate & battery-efficient. – dannyroa May 31 '13 at 18:20
  • I skipped the code that checks if Google Play Services is available. The % of users without Play Services are low especially if you're targeting 4.0+. – dannyroa May 31 '13 at 18:24
  • Okay, so besides adding the Toast.makeText() in onDisconnected and onConnected, all I need to do is write: mLocationClient = new LocationClient(this, this, this); mCurrentLocation = mLocationClient.getLastLocation(); – NumenorForLife May 31 '13 at 18:28
  • Yup. I forgot about the times when getLastLocation will be null. You need to modify MyLocation (from http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a) to use Play Services. I have to do it for my app too. I'll post it here when I'm done. – dannyroa May 31 '13 at 21:07