April 17, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Mapping with Google APIs in Android, Page 4

  • Updated: February 15, 2012
  • 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;

mOverlayController.animateTo(mDefPoint);

mOverlayController.setCenter(mDefPoint);

mMapView.invalidate();

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

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

}

}

 

public class MyLocationListener implements LocationListener {

@Override

public void onLocationChanged(Location location) {

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

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

mGPSPoint = new GeoPoint(lat, lon);

}

 

@Override

public void onProviderDisabled(String provider) {

}

 

@Override

public void onProviderEnabled(String provider) {

}

 

@Override

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



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel