February 10, 2012
Hot Topics:
RSS RSS feed

Mapping with Google APIs in Android

  • June 18, 2008
  • By Chunyen Liu
  • Send Email »
  • More Articles »

The overlay just created needs to be added to MapView's overlay controller before it goes into effect. Here is the code segment showing how you set up the overlay controller.

// Set up the overlay controller
mOverlayController = mMapView.createOverlayController();
MyOverlay mo = new MyOverlay();
mOverlayController.add(mo, true);

Figure 4: Label overlay on the map

Integrating with the Current GPS Location

android.location is the package that contains APIs allowing you to query the list of location providers as well as registering 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 immediately responsive. The following code segment gets the GPS position from the provider and then feeds it to MapView's controller.

private void centerOnGPSPosition() {
   String provider = "gps";
   LocationManager lm =
      (LocationManager)getSystemService(Context.LOCATION_SERVICE);

   // NOTE: When LocationManager is called the first time,
   //       lat / lon is initialized to 0.
   //       Subsequent calls are fine and fast.
   Location loc = lm.getCurrentLocation(provider);

   mDefPoint = new Point((int)(loc.getLatitude()  * 1000000),
                         (int)(loc.getLongitude() * 1000000));
   mDefCaption = "GPS location";

   mMapView.getController().animateTo(mDefPoint);
   mMapView.getController().centerMapTo(mDefPoint, true);
}

Figure 5: GPS info overlay on the map





Comment and Contribute

 


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

 

 


Networking Solutions
Sitemap | Contact Us