Hello everyone.
In this post, it will be presented the concept of Maps in Android.
To run a map application in Android, besides the creation of it's source code (final step), we need to follow some basic steps, which are: configure the project for the GoogleAPI's SDK; include the Google Maps library, and the Internet permission in the AndroidManifest.xml; get the access key of the Google Maps. So, let's start this steps.
Step 1
The first thing we need to do is to configure our project to some GoogleAPI SDK. What this means? Means that we need to include some SDK that have the map's libraries of Google. This can be done like shown below.
Now, we just need to follow the steps described here to create our project.
Step 2
To insert a map in a screen, we need to include the package com.google.android.maps.MapView. This package isn't default in Android. To import it, we need to add a reference for him in the AndroidManifest.xml file. This can be done using the tag <uses-library>. We also need to give Internet permission to our application, using the tag <uses-permission android:name="android.permission.INTERNET"/>. This 2 modifications of the AndroidManifest.xml file are shown below.
Step 3
Now we need to get the access key to Google Maps. For this, we need to access this site and enter with the fingerprint code of your computer. This fingerprint can be obtained with the help of the file debug.keystore. The location of this file is different in each operational system:
- Windows XP → C:\Documents and Settings\<user>\Local Settings\Application Data\Android\debug.keystore
- Windows Vista → C:\Users\<user>\AppData\Local\Android\debug.keystore
- Windows 7 → C:\Users\<user>\.android\debug.keystore
- OS X e Linux → ~/.android/debug.keystore
The location of this file can be found at Eclipse, through the menus Windows → Preferences → Android → Build, as shown below.
Now that we now the location of debug.keystore, we can generate the fingerprint of the Google Maps access key. This fingerprint can be generated through the command below (for any operational system):
C:\> keytool -v -list -alias androiddebugkey -keystore <local_debug_keystore> -storepass android -keypass android
OBS.: In the case above, the command was typed in Windows.
After this command be executed, will be generated the "Certificate fingerprint (MD5)", as shown below.
Now, to we get the Google Maps key, we need to access this site. If you don't have a Gmail account, it would be necessary to create one. Otherwise, we just need to login and type the fingerprint in the field "My certificate's MD5 fingerprint:" and press the button "Generate API Key". After type this button, another screen will be shown as result, as shown below.
In my case, the key is 03NHxysjybHVX2sHs87S1zn4LGQw20BuQ_PuOGQ. This is the string that I must use (in my machine) to use the class com.google.android.maps.MapView.
To pass this call to our application, there is two ways: through Java source code or through XML layout:
- Java → We must use in the event onCreate() of the Activity that extends the class MapActivity, as shown below.
- XML → We must use in the MapView of our XML layout file, as shown below.
Now we can start to write in the source code of our application.
Step 4
To we create a screen with maps, we need to create a child Activity of com.google.android.maps.MapActivity. Then, we need to create a component of the type com.google.android.maps.MapView in the event onCreate() and call the method setContentView().
Below are shown the Java and the XML files, that together enable the use of maps in Android. In this specific case, the maps API key was declared in the layout XML file.
The generated application is shown below.
Above the maps API was declared using the XML file. If it was declared in the Java file, we wouldn't need the XML file. So, the Java file would be as follows.
The example above was the most basic possible. Below are shown more interesting examples, and in the next topic of this blog, more useful examples.
An interesting example is to set a coordenate into the center of a map. For this, we use the class com.google.android.maps.GeoPoint, which represents a location through latitude and longitude coordenates. Also, they must use the microdegrees notation. This notation reffers to a grade multiplied by 1E6, which is the same of 1.000.000. This multiplication is made to force the generated value to be numeric, without float values.
A good example of this funcionality is shown below. In this example, a coordenate is centered in the map, and is given a zoom of factor15 in this coordenate. In Android, the zoom values varies from 1 to 21.
The result is shown below.
-----
That's all. Now we can call maps in our Android applications. There are many other map options. However, they'll be seen in other post's of this blog.
If someone has a question, suggestion or criticism, feel at ease.