Monday, November 28, 2011

Maps in Android


Android Maps
Google provides in the package "com.google.android.maps" a library for using Google Maps in Android. Google Maps is not part of the standard Open Source Platform Android and you require an additional key to use them. This key will be specified in the view which will be used to display the map.

You have to use library in manifest file to register maps in to your application
<uses-library android:required="true" android:name="com.google.android.maps"></uses-library>

Generate MD5 fingerprint
To generate an MD5 fingerprint of the debug certificate, first locate the debug keystore. By default, build tools create the debug keystore in the active AVD directory. The location of the AVD directories varies by platform:
·         Windows Vista: C:\Users\<user>\.android\debug.keystore
·         Windows XP: C:\Documents and Settings\<user>\.android\debug.keystore
·         OS X and Linux: ~/.android/debug.keystore

And use the following command to get the fingerprint
$ keytool -list -alias androiddebugkey \
-keystore <path_to_debug_keystore>.keystore \
-storepass android -keypass android

For Example
C:\Program Files\Java\jre6\bin>keytool -list -alias androiddebugkey -keystore "C
:\Users\suresh\.android\debug.keystore" -storepass android -keypass andr
oid
And you will get output like this
Certificate fingerprint (MD5): 0D:D9:8F:9C:58:D8:58:54:4D:82:D7:40:D4:FF:1A:1E
Copy above fingerprint and load this page in browser and paste in given editbox and click generate api key.

after this you will get output like this

Your key is:
0SE45C9JK4zfke2VFitVTpM-X6SfnVqJ1BHe2nQ
This key is good for all apps signed with your certificate whose fingerprint is:
0D:D9:8F:9C:58:D8:58:54:4D:82:D7:40:D4:FF:1A:1E
Here is an example xml layout to get you started on your way to mapping glory:
              <com.google.android.maps.MapView
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:apiKey="0SE45C9JK4zfke2VFitVTpM-X6SfnVqJ1BHe2nQ"
                 />
            




Use this api key in your layout xml file.


Then create one Class which should extended from MapActivity.


The class "MapActivity" extends the class "Activity" and provides the life-cycle management and the services for displaying a Map in a "MapView".

A "MapView" is typically defined in the layout for the MapActivity and requires the API key in the attribute android:apiKey. A MapView can be used with other UI components in the same layout.
Example

Public class MapsDemo extends MapActivity
{
Public void onCreate(Bundle b)
{
Super.onCreate(b);
setContentView(R.layout.main);
}
}



Note:

1)    In case you want to use Google Maps in your emulator you have to create a device which supports the Google API's

Advertisement