Google Maps tutorial for Android

You can use Google Maps library to insert Google Maps for your Android apps. I’m using Eclipse to develop this simple application.

Step 1. Install Google APIs from Android SDK Manager

 

Step 2. Select Google APIs as the Project Build Target

Step 3. Create a new Project.

  1. Add the following line into the application tag of the android manifest file. You have to do this because Maps is not a part of the standard library. <uses-library android:name=”com.google.android.maps” />
  2. We need internet access to retrieve maps. So request the INTERNET permission by adding  <uses-permission android:name=”android.permission.INTERNET”/>
  3. Open the main.xml file and add the MapView to the map.
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="Your API key"
android:clickable="true" />

Step 4. Obtaining the maps API key

To get the API key, you need to find the debug.keystore file first. In Linux it’s located  ~/.android/. Navigate to this place in a Command line and type keytool -list -alias androiddebugkey -storepass android -keypass android -keystore debug.keystore

Now you have your MD5 fingerprint. Then go to https://developers.google.com/android/maps-api-signup and enter your MD5 fingerprint to obtain your API key. It should look something similar to this, 0Ih1CuQaBILMPU8yU46CbkLnvcxXy8XL-7a0r_Q. Then you can use this in your MapView.

<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0Ih1CuQaBILMPU8yU46CbkLnvcxXy8XL-7a0r_Q"
android:clickable="true" />

Official documentation about this can be found in https://developers.google.com/maps/documentation/android/mapkey.

Step 5. Preparing the Activity

Open the Activity which you need to add the Map. You have to extend this from com.google.android.maps.MapActivity instead of android.app.Activity. Inside every MapActivity isRouteDisplayed() method is required. So add it to the class.

@Override
protected boolean isRouteDisplayed() {
return false;
}

Run the application now and see the world map in the main activity. You can move around the map and pinch to zoom in and out.

Step 6. Adding overlay items

Now you have a blank map. But you may need to add your own markers to the map. You must implement ItemizedOverlay class to do this. Create a new class called MyItemizedOverlay that extends com.google.android.maps.ItemizedOverlay. You need a list of OverlayItem objects in this class. Those are the individual points on the map.

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

  • Define the constructor,
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
  • Add a method to add an OverlayItem to the ArrayList. You must call the populate() method after an item is added.
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
  • Override the createItem(int i) and size() methods,
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}

Now your MyItemizedOverlay class is ready.

Step 6. Navigating in the Map

Go back to the MainActivity class. Add the following code. First we get the list pf overlays of the map view. Then we create a new ItemizedOverlay using a drawable as the marker.

MapView mapView = (MapView) findViewById(R.id.mapview);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.map_pin); 
MyItemizedOverlay overlay = new MyItemizedOverlay(drawable);

Now create a GeoPoint and using that create an OverlayItem. Then create an OverlayItem from this GeoPoint,

GeoPoint point = new GeoPoint(7000000, 81000000);
OverlayItem overlayitem = new OverlayItem(point, "Ayubowan!", "I'm in Sri Lanka!");
Add this OverlayItem to the map like this,
overlay.addOverlay(overlayitem);
mapOverlays.add(overlay);

You can animate to a point by calling the animateTo method in MapController.

mapView.getController().animateTo(point);

When you run the application the map will animate to the given GeoPoint. It should look like this.