January 21, 2021
Hot Topics:

Mapping with Google APIs in Android, Page 4

  • Updated:
  • By Chunyen Liu
  • Send Email »
  • More Articles »

Integrating with the Current GPS Location

The android.location package contains APIs allowing you to query the list of location providers as well as register for periodic updates of current positions. Each location provider maintains the files under the /data/misc/location/<provider_name> directory. Therefore, the default mock GPS provider "gps" can be found on the emulator in /data/misc/location/gps/. Different providers can generate the GPS files in different formats. More info can be found at Android's site in the references. Please note that, when you try to activate LocationManager the first time, it takes longer to initialize. Subsequent calls are more responsive. LocationListener updates mGPSPoint whenever location is changed. The following code segment gets the GPS position from the provider and then feeds it to MapView's controller.

private void centerOnGPSPosition() {

if (null == mGPSPoint) {

Toast.makeText(TutorialOnMaps.this, "No GPS info", Toast.LENGTH_LONG).show();

} else {

mDefPoint = mGPSPoint;




Toast.makeText(TutorialOnMaps.this, "(" + mDefPoint.getLatitudeE6() + ", " +

mDefPoint.getLongitudeE6() + ")", Toast.LENGTH_LONG).show();




public class MyLocationListener implements LocationListener {


public void onLocationChanged(Location location) {

int lat = (int)(location.getLatitude() * 1E6);

int lon = (int)(location.getLongitude() * 1E6);

mGPSPoint = new GeoPoint(lat, lon);




public void onProviderDisabled(String provider) {




public void onProviderEnabled(String provider) {




public void onStatusChanged(String provider, int status, Bundle extras) {



Figure 5 shows the results.

Click here for larger image

Figure 5. GPS Info Overlay on the Map

Page 4 of 5

This article was originally published on February 15, 2012

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date