Playing with Locations in Android

kapil sharma
3 min readNov 5, 2017

--

Two terms (Location API and Fused Location API) play very important role in locations for Android. Lets see what these terms means:-

Android’s Location API (Location Manager)

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.

Google’s Location Services API (Google Play Service) / Fused Location API

Google’s Location Services API is a part of the Google Play Services APK. They’re built on top of Android’s Location 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.

Getting device’s current/ last-known location using Google Play Service

Step1: Implement listeners

  • ConnectionCallbacks interface has two methods onConnected() & onConnectionSuspended()
  • ConnectionFailedListener interface has only one method onConnectionFailed()

Step2: Check for the required Permissions and obtain dynamic permissions

Step3: Create GoogleApiClient instance in onCreate() method

Step4: Now call connect() on GoogleApi

Step5: To get last know location write below code in onConnect method of ConnectionCallbacks

Getting Location updates:

Step1: In the above code only implement one more LocationListener and override its method.

public void onLocationChanged(final Location location)

Step2: Create location request with important attributes

If you want to get Accurate data LocationRequest.PRIORITY_HIGH_ACCURACY will be used as a parameter. Keep in mind the above will request location only from gps sensor in your device. If you want to work with network wifi or cellular data use LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY

Step3: Now request for the location updates: On every location update you will receive location in onLocationChanged() method.

LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);

Converting a Location(lat/long) to human readable address

Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code.

Step1: Create Geocoder instance

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

Step2: Check if Geocoder is present or not

isPresent()

Step3: call getFromLocation which will return List<Address>

getFromLocation(double latitude, double longitude, int maxResults)

Google Places API:

Getting location(lat/long) from the place which is picked by user

There are two ways of doing it:

  • Place Picker: User will pick location from map UI
  • Place Autocomplete: User will search using autocomplete

To see the sample code, please follow this link. This page has given sample code for both the ways which is easy to implement.

--

--

Responses (1)