Google Maps on Android – Part 1: Navigation

Integrating a google map on android is quiet simple – how to do this basically is shown in the tutorial on the android developer site.
Showing the map itself is one part, but most likely you want to interact with it in some way. The default map is completely zoomed out and centred somewhere over America. As this gives you a good idea how America looks like, it is not very useful for showing the location of the Synyx office. So what we need to do, is to move the map to some point and zoom in to a certain level. First, lets get the coordinates for the Synyx office. This can easily be done by using google maps: Navigate to maps.google.com, click on the “New” link on the top right and activate the “LatLng Marker” from the Google Maps Labs. This adds an option to the context menu to drop a marker that shows the latitude/ longitude values. Use those values to create a new GeoPoint in your Android app:

GeoPoint synyxOfficeLocation = new GeoPoint(49002175, 8394160);

As the GeoPoint handles its values in microdegrees, the values obtained from google maps must be multiplied with 100 000. Now as we have the GeoPoint, we need to centre the map to it and zoom in to a certain level. To perform this tasks, each MapView has a MapController:

MapController mapController = mapView.getController();
mapController.setCenter(synyxOfficeLocation);
mapController.setZoom(20);

This centres the map to the Synyx office. The zoom level must be a value between 1 (fully zoomed out) and 21 (fully zoomed in), so the value of 20 is already quiet close. Please note, that the highest zoom levels might not be available for all areas.
The above mentioned methods change the map in a very static way. For some more visual effects try animateTo(), zoomIn() and zoomOut() to change the view of the map by showing a short animation. An also very helpful method is zoomToSpan() which lets you define a latitude and longitude span that should be visible on the map – very handy if you want to ensure that two or more points are visible to the user on the map.

Kommentare

  1. Great and also have this example for drawing markers http://android-codes-examples.blogspot.com/2011/04/google-map-example-in-android-with-info.html