Search code examples
geolocationlocationlatitude-longitudegeofencingandroid-geofence

Get current location from the geo fence sample project


I was trying out the Google's Geo fence sample project. I'm getting the entry/exit events correctly. But how can I get the current location of the user from it. I would like to keep track of user location even after entry/exit points. Please help. The sample project is from the developer portal.


Solution

  • You'll need can get the user location from the Fused Location Provider in the normal fashion. The full tutorial is here: http://developer.android.com/training/location/retrieve-current.html

    But basically, when you receive the geofence event inside your Receiver or Service, get an instance of LocationClient there, connect to it, and in the callback, onConnected(), get the last known location:

    Location position = locationClient.getLastLocation();
    

    99.9% of the time, or a little better, you'll find this location to be inside your geofence.

    To continue keeping track of user location updates, follow the Receiving Location Updates: http://developer.android.com/training/location/receive-location-updates.html