Mapping with Google APIs in Android, Page 2
Panning, Zooming, and Toggling Map Modes
As was mentioned before, MapController is used to pan and zoom a map. You need to associate each button with a click listener. One example is as follows:
// Set up the button for "Pan East"
mPanE = (Button)findViewById(R.id.pane);
mPanE.setOnClickListener(new OnClickListener(){
// @Override
public void onClick(View arg0) {
panEast();
}
});
The panEast()
function is implemented to pan a quarter of the current screen width. Other panning functionalities are done in a similar fashion. Actually, the map itself is already draggable if the device is equipped with a touch screen input. For non-touch-screen devices, you use these buttons or the arrow keys of the rocker pad to pan the map.
public void panEast() {
GeoPoint pt = new GeoPoint(
mMapView.getMapCenter().getLatitudeE6(),
mMapView.getMapCenter().getLongitudeE6()
+ mMapView.getLongitudeSpan() / 4);mOverlayController.setCenter(pt);
mMapView.invalidate();
}
Zooming is done simply by increasing or decreasing the current zoom level through the controller.
public void zoomIn() {
mOverlayController.setZoom(mMapView.getZoomLevel() + 1);
mMapView.invalidate();
}
To toggle the map display for viewing satellite imagery or traffic, you can enable it directly from MapView.
public void toggleSatellite() {
if (mMapView.isSatellite())
mMapView.setSatellite(false);
else
mMapView.setSatellite(true);
mMapView.invalidate();
}
public void toggleTraffic() {
if (mMapView.isTraffic())
mMapView.setTraffic(false);
else
mMapView.setTraffic(true);
mMapView.invalidate();
}
Figures 2 and 3 show the results of toggling.
Click here for larger image
Figure 2. Satellite Imagery
Click here for larger image
Figure 3. Traffic on Satellite Imagery
Page 2 of 5
This article was originally published on February 15, 2012