Android : Google Maps Tutorial

Leave a Comment
The Android platform provides easy and tight integration between Android applications and Google Maps. The well established Google Maps API is used under the hood in order to bring the power of Google Maps to your Android applications. In this tutorial we will see how to incorporate Google Maps into an Android app.Installing the Google APIs
You can find more information about this procedure in the following links:
Google Maps API Key Generation
As you might know if you have used the Google Maps API in the past, a key is required in order to be able to use the API. The process is slightly different for use in Android applications, so let’s see what is required to do. Note that the process is described in the “Obtaining a Maps API Key” page, but I will also provide a description here.
First, we have to calculate the MD5 fingerprint of the certificate that we will use to sign the final application. This fingerprint will have to be provided to the Google Maps API service so that it can associate the key with your application. Java’s Key and Certificate Management tool named keytool is used for the fingerprint generation.
The keytool executable resides in the %JAVA_HOME%/bin directory for Windows or $JAVA_HOME/bin for Linux/OS X. For example, in my setup, it is installed in the “C:\programs\Java\jdk1.6.0_18\bin” folder.
While developing an Android application, the application is being signed in debug mode. That is, the SDK build tools automatically sign the application using the debug certificate. This is the certificate whose fingerprint we need to calculate. To generate the MD5 fingerprint of the debug certificate we first need to locate the debug keystore. The location of the keystore varies by platform:
  • Windows Vista: C:\Users\\.android\debug.keystore
  • Windows XP: C:\Documents and Settings\\.android\debug.keystore
  • OS X and Linux: ~/.android/debug.keystore
Now that we have located the keystore, we use the keytool executable to get the MD5 fingerprint of the debug certificate by issuing the following command:
keytool -list -alias androiddebugkey \

-keystore .keystore \
-storepass android -keypass android

For example, in my Windows machine I changed directory to the .android folder and I used the following command:
%JAVA_HOME%/bin/keytool -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android
Note that this was executed against the debug keystore, you will have to repeat this for the keystore that will be used with the application you are going to create. Additionally, the application is run on another development environment, with different Android SDK keystore, the API key will be invalid and Google Maps will not work.
The output would be something like the following:
androiddebugkey, Apr 2, 2010, PrivateKeyEntry,

Certificate fingerprint (MD5): 72:BF:25:C1:AF:4C:C1:2F:34:D9:B1:90:35:XX:XX:XX
This the fingerprint we have to provide to the Google Maps service. Now we are ready to sign up for a key by visiting the Android Maps API Key Signup page. After we read and accept the terms and conditions, we provide the generated fingerprint as follows:
We generate the API key and we are presented with the following screen:
Creating the Google Maps application
Finally, its time to write some code. Bookmark the Google APIs Add-On Javadocs for future reference. Integrating Google Maps is quite straightforward and can be achieved by extending the MapActivity class instead of the Activity class that we usually do. The main work is performed by a MapView which displays a map with data obtained from the Google Maps service. A MapActivity is actually a base class with code to manage the boring necessities of any activity that displays a MapView. Activity responsibilities include:
  • Activity lifecycle management
  • Setup and teardown of services behind a MapView
To extend from MapActivity we have to implement the isRouteDisplayed method, which denotes whether or not we are displaying any kind of route information, such as a set of driving directions. We will not provide such information, so we just return false there.
In our map activity, we will just take reference of a MapView. This view will be defined in the layout XML. We will also use thesetBuiltInZoomControls method to enable the built-in zoom controls.
And here is what the application screen looks like:
If you click inside the map, the zoom controls will appear and you will be able to zoom in and out.
Adding map overlays
The next step is to add some custom map overlays. To do so, we can extend the Overlay class, which is a base class representing an overlay which may be displayed on top of a map. Alternatively, we may extend the ItemizedOverlay, which is a base class for an Overlaywhich consists of a list of OverlayItems. Let’s see how we can do this (note that the following example is very similar to the Hello Map Viewarticle from the Android documentation).
Finally, we take reference of the underlying MapController and use it to point the map to a specific geographical point using theanimateTo method and to define the zoom level by using the setZoom method.
If we launch again the configuration, we will be presented with a zoomed-in map which includes an overlay marker pointing toJavaCodeGeeks home town Athens, Greece. Clicking on the marker will cause the alert dialog to pop-up displaying our custom message.
That’s all guys. A detailed guide on how to integrate Google Maps into your Android application. As always, you can download the Eclipse project created for this tutorial.

0 comments:

Post a Comment