The GoogleMap class lets you integrate Google Maps into your application but you must register with the Google Maps service before your implementation will be able to obtain map data.
Registering and using a Google Maps API Key is free and has two parts:
We now outline how to implement these two steps; for more details, see Get the Google Maps API key.
To keep things simple we shall register using the debug certificate associated with our development machine to obtain a temporary Maps API key. This is adequate for demonstration and development. However, when you publish an app (e.g., deployment through the Google Play Store) you must digitally sign it and (if you employ Google Maps) before you publish your application you must register for a new Key based on your release certificate, and update the references in your app to this new key. More detail may be found at Signing Your Applications. |
To generate an SHA-1 fingerprint of the SDK debug certificate, we must first locate the debug keystore. This will depend on the platform in use. For example, some standard locations are
If in doubt, you can locate the debug keystore by using Eclipse and choosing Window > Preferences > Android > Build. On my Fedora Linux systems, this window will also display the SHA-1 fingerprint that we obtain below from the command line. |
Once you have located the keystore, the following command issued at a shell prompt will return the SHA-1 fingerprint of the debug certificate:
keytool -list -alias androiddebugkey -keystore <path_to_debug_keystore> -storepass android -keypass android
where you should substitute for <path_to_debug_keystore> the path to your keystore. For example, on one of my Linux systems (named M33) I obtained with this command
[guidry@m33 ~]$ keytool -list -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass android -keypass android
androiddebugkey, Dec 13, 2013, PrivateKeyEntry,
Certificate fingerprint (SHA1): 73:D5:27:C7:82:34:82:9A:6B:63:C4:D9:99:CA:29:EE:18:A2:29:1B
Copy and save the SHA-1 fingerprint, as we shall use it shortly to register with the Map service.
NOTE: The deprecated Version 1 of Google Maps used the MD5 fingerprint. Version 2 has switched to using the (generally thought more secure) SHA-1 fingerprint. Be certain that the fingerprint that you obtain by the above procedure is the SHA-1 fingerprint as the MD5 fingerprint will not work for current applications of Google Maps. |
Now use a browser to create an API project in the Google APIs Console.
Now we can request a Maps API key.
The server will add an entry to the page that contains the map key and will look like the following,
with the generated 40-character key displayed in the first line.
Now we need to insert a tag in the project Manifest file that specifies this key so that the maps server will know that you have permission to download map data. Copy the key from the webpage above, open the AndroidManifest.xml file for your project, and insert the following tag inside the <application>...</application> tag.
<!-- GoogleMaps api key-->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="api_key" />
where you should substitute for "api_key" the 40-character map key obtained above. This will make the maps API key visible to any MapFragment contained in your application. A general introduction to using Google Maps in Android may be found in the project Map Example. There we discuss additional permissions that must be declared in the manifest file and library accesses that must be enabled to use maps effectively.
This maps API key is valid only under the debug certificate on a specific machine (M33 in this example),
for as long as that debug certificate is valid.
|
The reason that all of this may seem to be more involved than it needs to be is basically that Google Maps are compatible with the Android API, but they are licensed separately from Android.
Last modified: Feb. 10, 2014