Android Location API to track your current location With Examples

Android Location API can be used to track your mobile current location and show in the app. In this tutorial, we’ll develop an application that fetches the user’s current location programmatically.

Android Location API

There are two ways to get a users location in our application:

  • android.location.LocationListener : This is a part of the Android API.
  • com.google.android.gms.location.LocationListener : This is present in the Google Play Services API. (We’ll look into this in the next tutorial)

Android Location Services is available since Android API 1.
Google officially recommends using Google Play Location Service APIs. Android Location Services API is still used to develop location-based apps for devices that don’t support Google Play Services.

LocationListener

The LocationListener interface, which is part of the Android Locations API is used for receiving notifications from the LocationManager when the location has changed. The LocationManager class provides access to the systems location services.

The LocationListener class needs to implement the following methods.

  • onLocationChanged(Location location) : Called when the location has changed.
  • onProviderDisabled(String provider) : Called when the provider is disabled by the user.
  • onProviderEnabled(String provider) : Called when the provider is enabled by the user.
  • onStatusChanged(String provider, int status, Bundle extras) : Called when the provider status changes.

The android.location has two means of acquiring location data:

  • LocationManager.GPS_PROVIDER: Determines location using satellites. Depending on the conditions, this provider may take a while to return a location fix
  • LocationManager.NETWORK_PROVIDER: Determines location based on the availability of nearby cell towers and WiFi access points. This is faster than GPS_PROVIDER

In this tutorial, we’ll create a Service that implements the LocationListener class to receive periodic location updates via GPS Providers or Network Providers.

Android Location API Project Structure

android-location-tracking-project-structure

The project consists of a MainActivity.java class which displays a Get Location and a LocationTrack.java Service class.

Android Location APICode

The activity_main.xml layout is defined below.

The MainActivity.java class is defined below.

In the above code, we’re implementing runtime permissions that are used in Android 6.0+ devices.
We’ve added the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in the AndroidManifest.xml file.
Clicking the button invokes the LocationTrack.java service class. If the location returned is NULL in the case of GPS Provider, we call the showSettingsAlert() method from the LocationTrack.java class that we’ll be seeing shortly. When the activity is destroyed stopLocationTrack() method is called to turn off the location updates.

The LocationTrack.java class is defined below.

Few inferences drawn from the above code are:

  • In the above code isProviderEnabled(String provider) is called upon the locationManager object and is used to check whether GPS/Network Provider is enabled or not.
  • If the Providers aren’t enabled we’re calling the method showSettingsAlert() that shows a prompt to enable GPS.
  • requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) method of the LocationManager class is used to register the current activity to be notified periodically by the named provider.
  • onLocationChanged is invoked periodically based upon the minTime and minDistance, whichever comes first.
  • Location class hosts the latitude and longitude.
    To get the current location the following code snippet is used.

On the above Location object, getters are called to store the double values of latitude and longitude. These double values are then disabled as a Toast message on the screen.

  • To stop location updates removeUpdates method is called on the LocationManager instance.

 

The output of the above application in action on an emulator is:
android location api, android location tracking

Our emulators can’t fetch locations, hence it’s returning 0.0 for lat/lng. You can connect your smartphone and run the application in debug mode to check your current location.
To emulate GPS Locations in an emulator we can pass fixed Latitude and Longitude values from Android Studio. Besides the emulator window, you can see a list of options. The one at the bottom (which has three dots) is our Extended Controls options. Open that and send a dummy location. Your application should look like this:
android location manager

This brings an end to this tutorial. We’ll be implementing Location API using Google Play Services in a later tutorial. You can download the Android GPSLocationTracking Project from the link below.

By admin

Leave a Reply

%d bloggers like this: