I want to build an app that centers around getting the user's current location and then find points of interest(such as bars,restaurants,etc) that are close to him/her via the Google Places API.
Upon searching the web for a place to start I came across some tutorials that use the LocationManager
class and some others that use Google Play Services in order to find the users location.
On first sight both of them do the same thing, but since I am new to this I got a little confused and I don't know which method suits my needs the best. So, I want to ask you :
What are the differences between these two methods of finding locations (if there are any) ?
Best Answer
User Location on Android
Getting the user’s location on Android is a little less straightforward than on iOS. To start the confusion, there are two totally different ways you can do it. The first is using Android APIs from
android.location.LocationListener
, and the second is using Google Play Services APIscom.google.android.gms.location.LocationListener
. Let’s go through both of them.Android’s Location API
The Android’s location APIs use three different providers to get location -
LocationManager.GPS_PROVIDER
— This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix.LocationManager.NETWORK_PROVIDER
— This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup.LocationManager.PASSIVE_PROVIDER
— This provider will return locations generated by other providers. You passively receive location updates when other applications or services request them without actually requesting the locations yourself.The gist of it is that you get an object of
LocationManager
from the system, implement theLocationListener
, and call therequestLocationUpdates
on theLocationManager
.Here’s a code snippet:
Google’s API Guide on Location Strategiesexplains the code pretty nicely. But they also mention that in most cases, you’ll get better battery performance, as well as more appropriate accuracy, by using the Google Location Services API instead. Now the confusion starts!Google’s Location Services API is a part of the Google Play Services APK (here’s how to set it up) . They’re built on top of Android’s API. These APIs provide a “Fused Location Provider” instead of the providers mentioned above. This provider automatically chooses what underlying provider to use, based on accuracy, battery usage, etc. It is fast because you get location from a system-wide service that keeps updating it. And you can use more advanced features such as geofencing.
To use the Google’s Location Services, your app needs to connect to the
GooglePlayServicesClient
. To connect to the client, your activity (or fragment, or so) needs to implementGooglePlayServicesClient.ConnectionCallbacks
andGooglePlayServicesClient.OnConnectionFailedListener
interfaces. Here’s a sample code:locationClient.getLastLocation()
null?The
locationClient.getLastLocation()
gets the last known location from the client. However, the Fused Location Provider will only maintain background location if at least one client is connected to it. Once the first client connects, it will immediately try to get a location. If your activity is the first client to connect and you callgetLastLocation()
right away inonConnected()
, that might not be enough time for the first location to come in. This will result inlocation
beingnull
.To solve this issue, you have to wait (indeterminately) till the provider gets the location and then call
getLastLocation()
, which is impossible to know. Another (better) option is to implement thecom.google.android.gms.location.LocationListener
interface to receive periodic location updates (and switch it off once you get the first update).In this code, you’re checking if the client already has the last location (in
onConnected
). If not, you’re requesting for location updates, and switching off the requests (inonLocationChanged()
callback) as soon as you get an update.Note that the
locationClient.requestLocationUpdates(locationRequest, this);
has to be inside theonConnected
callback, or else you will get anIllegalStateException
because you will be trying to request for locations without connected to the Google Play Services Client.Many times, the user would have location services disabled (to save battery, or privacy reasons). In such a case, the code above will still request for location updates, but
onLocationChanged
will never get called. You can stop the requests by checking if the user has disabled the location services.If your app requires them to enable location services, you would want to show a message or a toast. Unfortunately, there is no way of checking if the user has disabled location services in Google’s Location Services API. For this, you will have to resort back to Android’s API.
In your
onCreate
method:And use the
locationEnabled
flag in youronConnected
method like this:UPDATE
Document is updated, LocationClient is removed and the api supports to enable GPS with one click from dialog:
Link https://developer.android.com/training/location/change-location-settings#prompt
New location client: FusedLocationProviderClient
It is recommended to go through https://developer.android.com/training/location before doing any location tasks.