Location API in Android


This tutorial describes the usage of the Android Location API to fetch the device location i.e., Longitude and Latitude using available providers in the device.

 

Nearly all Android devices posses GPS in them and you can make use of it to discover the location of your user. Location recognition applications are smarter and offer better information.

Take for instance, you are hungry; you open your android device and it tells you that a food canteen is 30 meters away from your current location and illustrates to you the precise location. That’s the kind of thing that location recognition applications can do.

This can be done via a GPS (Global Positioning System) module, via cell tower triangulation or via wifi networks. Android contains the android.location package which provides the API to determine the current geo position.

 

LocationManager

The LocationManager class provides access to the Android location service. This services allows to access location providers, to register location update listeners and proximity alerts and more.

 

LocationProvider

The LocationProvider class is the superclass of the different location providers which deliver the information about the current location. This information is stored in theLocation class.

The Android device might have several LocationProvider available and you can select which one you want to use. In most cases you have the followng LocationProvider available.

LocationProvider Description

network

Uses the mobile network or WI-Fi to determine the best location. Might have a higher precision in closed rooms then GPS.

LocationManager.NETWORK

gps

Use the GPS receiver in the Android device to determine the best location via satellites. Usually better precision than network.

LocationManager.GPS_PROVIDER

passive

This provider is special, in that it indicates that your app doesn’t want to actually initiate a location fix, but uses the location updates received by other applications/services. In other words, the passive provider will use location data provided by either the GPS or NETWORK providers. You can find out what provider your passive provider actually used with the returned Location’s getProvider() method. This provides the greatest battery savings.

LocationManager.PASSIVE_PROVIDER

 

Selecting LocationProvider via Criteria

For a flexible selection of the best location provider use a Criteria object, in which you can define how the provider should be selected.

 

Permissions

If you want to access the GPS sensor, you need the ACCESS_FINE_LOCATION permission. Otherwise you need the ACCESS_COARSE_LOCATION permission.

 

Prompt the user to Enabled GPS

The user can decide to enabled GPS or not. You can find out, if a LocationManager is enabled via the isProviderEnabled() method. If its not enabled you can send the user to the settings via an Intent with theSettings.ACTION_LOCATION_SOURCE_SETTINGS action for theandroid.provider.Settings class.

 

REQUESTING LOCATION UPDATES

LocationListener

The LocationListener is an interface for receiving Location updates from the LocationManager. It has four methods

  • onLocationChanged() – called whenever there is an update from the LocationManager.
  • onStatusChanged() – called when the provider status changes, for example it becomes available after a period of inactivity
  • onProviderDisabled() – called when the user disables the provider. You might want to alert the user in this case that your app functionality will be reduced
  • onProviderEnabled() – called when the user enables the provider

 

We will need to call the method requestLocationUpdates to get the current location as it’s updated by the user.

The parameters of this function are as follows:

provider the name of the provider with which we would like to regiser.
min Time minimum time interval between location updates (in milliseconds).
min Distance minimum distance between location updates (in meters).
listener a LocationListener whose onLocationChanged(Location) method will be called for each location update.

 

 

Sample Activity:

 

ScreenShot:

Complete Source code available here.


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.