Saturday, 6 November 2010

Google Maps in Android (emulator)

In order to get google maps in your apps and so build location-based services, your must register with Google, and include a key that they'll provide against a MD5 string that you get from your own system. If you're like me, just developing and debugging without deploying your apps, you'll need one key called "debug.store", another one will be used to comercially deploy your apps.

I've done that using Eclipse, so here it goes. Locate your .android folder, on Ubuntu/Linux, that's in ~/.android, it's a hidden file, (hence the dot .android, type ls -a for hidden files), on Windows 7, its location is at C:\Users\\.android. This file contains a digital certificate that is going to be used to create the MD5 string. You need Keytool in Eclipse, Eclipse is the "does it all for you" and is getting really powerful so if you're concerned with efficiency in producing code, Eclipse is the main thing.
To install keytool, once again go to the Help menu, install new software (in Helios), click add for a new repository, add http://keytool.sourceforge.net/update, (check http://keytool.sourceforge.net/), then follow the usual procedure to install plugins in Eclipse.

Once installed, they'll be a menu called keystore, choose open keystore, browse to the "debug.store" in the .android folder, and type "android" as password. It took me a while to find the password ... In the lower part of eclipse, where the console usually is, select the keytool tab if it's not already, double click on "androiddebugkey", the certificate should open and the MD5 Fingerprint displayed. Copy it and go to Maps API Key Signup - http://code.google.com/android/add-ons/google-apis/maps-api-signup.html, you need a valid Google account for that, accept their terms and conditions, and paste the key where it says so. It should give you back the key to insert in your code. That's in your project folder in Eclipse, open main.xml located in the res then layout folder and where it says apiKey, insert it. Now theoretically, it should display a Google map in your app. And it does!

Your main.xml should look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<com.google.android.maps.MapView
android:id="@+id/mapview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="Your API Key" />
</LinearLayout>

Eclipse + Android

2 comments:

Breisa said...

Very helpful... :D Thank you so much.

asianuxxx said...

Thanks.