Monday, March 22, 2010

Map in Android

Earlier when the first SDK of androd released, google map is a part of SDK. Now, its not coming with SDK, its coming as add-on.

First for for displaying google map for android application, we need Google map API Key. For this, using the keytool in the JDK, we need to create the finger-print.

We will get the keyStore from eclipse. The path is


Window->Preferences->Android->Build


I can show a a sample command for getting the finger print in windows xp is,


C:\Program Files\Java\jre1.5.0_06\bin>keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Acer\.android\debug.keystore" -storepass android -keypass android



Type this command in dos prompt and you will get the finger print. Then using this finger-print, we need to get the map api key from the url

Sign Up


After getting the API key, use this API key to display map. Then at the time of creating Android application, we need to select the target as Google API 4 or whichever version we need. It is importand that you create an AVD with target as Google API level 3 or greater.

Then for displaying map, we need to extend MapActivity.

For displaying the zoomIn comtrols,


setBuiltInZoomControls(true);


For making the map clickable and focusable, do


setClickable(true);
setFocusable(true);


For animating to a particular point in map use,


MapController mc = mapView.getController;
mc.animateTo(geoPoint);


For making a point to center, use

mc.setCenter(geoPoint);


For setting the zoom level use,

mc.setZoom(value);


For drawing over map use Overlay or ItemizedOverlay. Here we are discussing about the ItemizedOverlay.

Using this, we can easily put marker in the map.

Create another class called MyItemizedOverlay which extends ItemizedOverlay which overrides two functions createItem() and size().

Then we create an ArrayList of OverlayItem. In addition, we create a function called addItem(), in that we are adding OverlayItem to the list and calling populate().

Now our MyItemizedOverlay class will look like this,


class MyItemizedOverlay extends ItemizedOverlay {

private ArrayList mOverlays = new ArrayList();
Context ctContext;

public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
ctContext = context;
}

public void addItem(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}

@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}

@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
}


Then, on tapping the marker, to display some information, I will override onTap() function and create a dialog for displaying the details. The onTap() function will look like this.


protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
Dialog dialog = new Dialog(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}

No comments:

Post a Comment

Androidians