Advertisement

GETTING STARTED WITH GOOGLE MAPS IN ANDROID

HTML clipboard

In this article, I will show you how to develop a simple Android application that makes use of Google Maps service and how to perform operations like zoom in, zoom out and changing the view of the map object.

I am using Eclipse/ADT(Android Development Tools) with android-sdk 1.6. Before we start our coding, we need to obtain a Maps API key.
Obtaining a Maps API key:

Since you are trying to obtain data from Google Maps service, you need to register for free with Google Maps by agreeing the Terms of Service. Without signing up for Maps API key, you cannot use Google Maps in your Android application. 

There are two simple steps involved in getting the Maps API key.

1) Obtain the MD5 fingerprint of the SDK debug certificate using the program “keytool.exe” present in jdk. You can use the following command for doing so.

C:\>"C:\Java\jdk1.5\bin\keytool.exe" -list -alias androiddebugkey -keystore "C:\Documents and Settings\Jayaram\.android\debug.keystore" -storepass android -keypass android

If you are not sure about the path for the file “debug.keystore”, go to 
Window->Preferences->Android->Build

in your eclipse window and find the full path.

After successfully executing the above command, you’ll get a result as shown below. 



2) Then, open your web browser and go to,

http://code.google.com/android/maps-api-signup.html

You need to sign in with your Google account for registering Maps API key. Just copy the md5 fingerprint you just obtained using the above command, agree the terms of service and click “Generate API key” button. You will get a page with your unique API key like this.



Using the Maps API key in our application:

We can use our unique API key to create MapView objects in our application in two ways. We can either create it using “main.xml” file as given below or we can create MapView objects directly in our program using the constructor like,
MapView mymapview = new MapView(this,”<your api key>”);
In our application, we are using “main.xml” to achieve this.
main.xml:

<?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="0DH90GX2te-UlRDQaMdq7RsiX5P7qDwz7p7FJwQ" /> 
</LinearLayout>


Setting Internet Permissions and Using external libraries:
Since our android application is using the Internet and Google Maps API library, we need to make a couple of entries in our “AndroidManifest.xml” file.
For using the Internet, we need to include the following line inside the “<manifest>” tag.
<uses-permission android:name="android.permission.INTERNET" /> 

For including the maps library, include the following line inside the “<application>” tag.

<uses-library android:name="com.google.android.maps" />

So, our final “AndroidManifest.xml” file will look like this.

AndroidManifest.xml: 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mypack.mydemos"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps"/>
<activity android:name=".mapdemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />

</manifest>


Performing operations on Map using the java code:
For displaying a particular location in your MapView and performing various operations on it, you just need to replace the default “mapdemo.java” file with the following one.

mapdemo.java

import android.os.Bundle;
import android.view.KeyEvent;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.GeoPoint;

public class mapdemo extends MapActivity {
private MapView myMapView;


@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myMapView = (MapView)findViewById(R.id.mapview1);

// These points represents the location of my //college. I grabbed the data from Google maps.
GeoPoint p = new GeoPoint((int) (10.090834 * 1000000),
(int) (78.795441 * 1000000));
// Get the controller, that is used for translation and zooming
MapController mc = myMapView.getController();
// Translate to my college mc.animateTo(p);
// set the zoom level
mc.setZoom(18);

// Enable Sattelite-Mode
myMapView.setSatellite(true);
}

// Perform various operations using keycode
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
if (keyCode == KeyEvent.KEYCODE_I) {
// Zooming In
myMapView.getController().setZoom(myMapView.getZoomLevel() + 1);

return true;
} else if (keyCode == KeyEvent.KEYCODE_O) {
// Zooming Out
myMapView.getController().setZoom(myMapView.getZoomLevel() - 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_S) {
// Switch to satellite view
myMapView.setSatellite(true);
return true;
} else if (keyCode == KeyEvent.KEYCODE_T) {
// Switch to traffic overlays
myMapView.setSatellite(false);
myMapView.setTraffic(true);
return true;
}
return false;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

For including Google Maps in our application, our class needs to extend the MapActivity class. Since we have done this, we must override the isRouteDisplayed() method of the MapActivity class.

The MapController class provides several methods for performing operations on maps such as panning and zooming. We can immediastely translate to a desired location by creating a GeoPoint and using the animateTo() method of the MapController class. For doing so, we need to supply the latitude and longitude details in int to the GeoPoint constructor. For our application, i grabbed the data about my college’s location using Google Maps, so that my college will show up, once you run the application.

For running this application, create an AVD with the Target name “Google APIs (API level 4)”. The immediate output of our application will look like,



We have performed several operations like toggling between satellite and traffic views, zoom in and out, using the onKeyDown() method.

We can switch to satellite view or traffic view using the methods setSatellite(boolean) and setTraffic(boolean) of the MapView class. In our application, we can switch to traffic view by pressing the key ‘T’ from our keyboard. A sample output after switching to the traffic overlays is given below.



For zooming in and out, we have used the setZoom() method of the MapController class. For zooming in, we have increased the current zoom level by 1 and for zooming out, we have decreased it by 1. 

Alternatively, we can use the zoomIn() and zoomOut() methods of the MapController class. 

The output of our application after zooming in is given below.

Summary:
From this article, I hope that you have learnt some basic ideas about working with Google Maps in Android. There are so many topics related to Google Maps such as geocoding, reverse geocoding, geotagging, gettracking etc., that you can work on. I will discuss those features in my next article. 

Author is studying as final year MCA student in Alagappa Chettiar College of Engg & Tech., Karaikudi. You can reach him from the following email-id. jaiaccet@gmail.com








Added on July 6, 2010 Comment

Comments

#1

sri harsha commented, on August 24, 2010 at 5:49 p.m.:

hi,
u have shown google maps. so, is it possible to access even other maps like openlayers or any 2d maps apart from google??

Post a comment