Using Global Positioning Services (GPS) - Android

The Android SDK provides means for accessing location via a built-in GPS hardware, when it’s available. Generally speaking, just about every Android phone has some LBS capabilities. For example, in the United States, mobile phone location information is used by emergency services. That said, not all Android devices are phones, nor do all phones enable consumer-usage of LBS services. If GPS features are disabled, or an Android device does not have LBS hardware, the Android SDK provides additional APIs for determining alternate location providers. These other providers might have advantages and disadvantages in terms of power use, speed, and accuracy of reporting.

Using GPS Features in Your Applications

LBS services and hardware such as a built-in precision GPS are optional features for Android devices. In addition to requiring the appropriate permissions, you can specify which optional features your application requires within the Android Manifest file. You can declare that your application uses or requires specific LBS services using the <usesfeature> tag of the Android Manifest file. Although this tag is not enforced by the Android operating system, it enables popular publication mechanisms such as the Android Market to filter your app and provide it only to users with appropriate devices. If your application will only function well on devices with some sort of method for determining the current location, you could use the following <uses-feature> tag in your application’s manifest file:

<uses-feature android:name="android.hardware.location" />

If your application requires a precise location fix (that is, the device has functional GPS hardware, not just cell tower triangulation or other such mechanisms), you could use the following <uses-feature> tag instead:

<uses-feature android:name="android.hardware.location.gps" />

New settings for the <uses-feature> tag have been added in recent Android SDK releases. For example, the values we’ve discussed, such as android .hardware .location, were added in Android 2.2 (API Level 8).

Finding Your Location

To determine device location, you need to perform a few steps and make some choices. The following list summarizes this process:

  1. Retrieve an instance of the LocationManager using a call to the getSystemService() method using the LOCATION_SERVICE.
  2. Add an appropriate permission to the AndroidManifest.xml file, depending on what type of location information the application needs.
  3. Choose a provider using either the getAllProviders() method or the getBestProvider() method.
  4. Implement a LocationListener class.
  5. Call the requestLocationUpdates() method with the chosen provider and the LocationListener object to start receiving location information.

Specific permissions are not needed to retrieve an instance of the LocationManager object. Instead, the permissions determine the available providers. The following code retrieves an instance of the LocationManager object:

import android.location.*;
LocationManager location =

The following block of XML provides the application with both coarse and fine location permissions when added within the AndroidManifest.xml permissions file:

android:name="android.permission.ACCESS_FINE_LOCATION" />
android:name="android.permission.ACCESS_COARSE_LOCATION" />

Now that the application has permissions to use location information and the LocationManager object is valid, we must determine what provider to use for location information. The following code configures a Criteria object and requests the provider based on this information:

Criteria criteria = new Criteria();
String bestProvider = location.getBestProvider(criteria, true);

The setAccuracy() method can take values for ACCURACY_COARSE and ACCURACY_FINE that can be used (along with the appropriate permissions) to request a provider that the application has permissions to use. You can use the setPowerRequirement() method to find a provider that fits certain power use requirements, such as POWER_HIGH or POWER_LOW. The Criteria object also enables us to specify if the provider can incur a monetary cost to the user, whether altitude is needed, and some other details. If the application has specific requirements, this is where you set them. However, setting these criteria doesn’t imply that the provider is available to the user. Some flexibility might be required to allow use on a broad range of devices. A Boolean parameter of the getBestProvider() method enables the application to ask for only enabled providers.

Using the provider returned by the getBestProvider() method, the application can request the location. Before doing so, however, the application needs to provide an implementation of LocationListener. The LocationListener implementation consists of four methods: To tell the application whether the provider has been disabled or enabled; to give the status about the provider (such as the number of satellites the GPS receiver can see); and to tell the application location information.The following is a sample implementation for the last method, the onLocationChanged() method:

public void onLocationChanged(Location location) {
String locInfo = String.
format("Current loc = (%f, %f) @ (%f meters up)",
location.getLatitude(), location.getLongitude(),
location.getAltitude() );
if (lastLocation != null) {
float distance = location.distanceTo(lastLocation);
locInfo += String.
format("\n Distance from last = %f meters", distance);
lastLocation = location;

The onLocationChanged() method receives a Location object with the most recent location information from the chosen provider. In this example, the application merely prints out the location, including the altitude, which might or might not be returned by the provider. Then, it uses a utility method of the Location object, distanceTo(), to calculate how far the handset has moved since the last time onLocationChanged() was called.

It is up to the application to determine how to use this location information. The application might want to turn the location information into an address, display the location on an embedded map, or launch the built-in Maps application (if the Google applications are installed) centered at the location.

Locating Your Emulator

The Android emulator can simulate location-based services, but as you would expect, it does not have any “underlying hardware” to get a real satellite fix. The Android SDK provides a means to simulate location data with the use of a single location point, GPX file, or KML file. This works only with the emulator, not the physical handset, but it can be useful for testing your location-based application.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics