Location and Maps

Location and Maps quickview

  • Android provides a location framework that your application can use to determine the device's location and bearing and register for updates.
  • A Google Maps external library is available that lets you display and manage Maps data.

In this document

  1. Location Services
  2. Google Maps External Library

See Also

  1. Google APIs add-on download»

Location- and maps-based applications and services are compelling for mobile device users. You can build these capabilities into your applications using the classes of the android.location package and the Google Maps external library. The sections below provide details.

Location Services

Android gives your applications access to the location services supported by the device through the classes in the android.location package. The central component of the location framework is the LocationManager system service, which provides an API to determine location and bearing if the underlying device (if it supports location capabilities).

As with other system services, you do not instantiate a LocationManager directly. Rather, you request an LocationManager instance from the system by calling getSystemService(Context.LOCATION_SERVICE). The method returns a handle to a new LocationManager instance.

Once your application has a handle to a LocationManager instance, your application will be able to do three things:

  • Query for the list of all LocationProviders known to the LocationManager for its last known location.
  • Register/unregister for periodic updates of current location from a LocationProvider (specified either by Criteria or name).
  • Register/unregister for a given Intent to be fired if the device comes within a given proximity (specified by radius in meters) of a given lat/long.

However, during initial development in the emulator, you may not have access to real data from a real location provider (Network or GPS). In that case, it may be necessary to spoof some data for your application using a mock location provider.

Note: If you've used mock LocationProviders in previous versions of the SDK, you can no longer provide canned LocationProviders in the /system/etc/location directory. These directories will be wiped during boot-up. Please follow the new procedures outlined below.

Providing Mock Location Data

When testing your application on the Android emulator, there are a couple different ways to send it some mock location data: you can use the DDMS tool or the "geo" command option in the emulator console.

Using DDMS

With the DDMS tool, you can simulate location data a few different ways:

  • Manually send individual longitude/latitude coordinates to the device.
  • Use a GPX file describing a route for playback to the device.
  • Use a KML file describing individual placemarks for sequenced playback to the device.

For more information on using DDMS to spoof location data, see the Using DDMS guide.

Using the "geo" command in the emulator console

Launch your application in the Android emulator and open a terminal/console in your SDK's /tools directory. Connect to the emulator console. Now you can use:

  • geo fix to send a fixed geo-location.

    This command accepts a longitude and latitude in decimal degrees, and an optional altitude in meters. For example:

    geo fix -121.45356 46.51119 4392
  • geo nmea to send an NMEA 0183 sentence.

    This command accepts a single NMEA sentence of type '$GPGGA' (fix data) or '$GPRMC' (transit data). For example:

    geo nmea $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62

For information about how to connect to the emulator console, see Using the Emulator Console.

Google Maps External Library

To make it easier for you to add powerful mapping capabilities to your application, Google provides a Maps external library that includes the com.google.android.maps package. The classes of the com.google.android.maps package offer built-in downloading, rendering, and caching of Maps tiles, as well as a variety of display options and controls.

The key class in the Maps package is com.google.android.maps.MapView, a subclass of ViewGroup. A MapView displays a map with data obtained from the Google Maps service. When the MapView has focus, it will capture keypresses and touch gestures to pan and zoom the map automatically, including handling network requests for additional maps tiles. It also provides all of the UI elements necessary for users to control the map. Your application can also use MapView class methods to control the MapView programmatically and draw a number of Overlay types on top of the map.

In general, the MapView class provides a wrapper around the Google Maps API that lets your application manipulate Google Maps data through class methods, and it lets you work with Maps data as you would other types of Views.

The Maps external library is not part of the standard Android library, so it may not be present on some compliant Android-powered devices. Similarly, the Maps external library is not included in the standard Android library provided in the SDK. So that you can develop using the classes of the com.google.android.maps package, the Maps external library is made available to you as part of the Google APIs add-on for the Android SDK.

To learn more about the Maps external library and how to download and use the Google APIs add-on, visit

http://code.google.com/android/add-ons/google-apis

For your convenience, the Google APIs add-on is also included in the Android SDK.

Note: In order to display Google Maps data in a MapView, you must register with the Google Maps service and obtain a Maps API Key. For information about how to get a Maps API Key, see Obtaining a Maps API Key.

↑ Go to top