Google

Monday, July 12, 2010

Android Map API Overlay



This example puts a mapmarker (orange teardrop balloon image) on IICP.

You need to put the mapmarker.png image in res/drawable folder.


MappingOverlayActivity.java

 package com.moa;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MappingOverlayActivity extends MapActivity {
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
Drawable marker=getResources().getDrawable(R.drawable.mapmarker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
InterestingLocations funPlaces = new InterestingLocations(marker);
mapView.getOverlays().add(funPlaces);
GeoPoint pt = funPlaces.getCenter(); // get the first-ranked point
mapView.getController().setCenter(pt);
mapView.getController().setZoom(15); // cheating. We could iterate
// and figure out a proper zoom.
}
@Override
protected boolean isLocationDisplayed() {
return false;
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public void myClickHandler(View target) {
switch(target.getId()) {
case R.id.sat:
mapView.setSatellite(true);
break;
case R.id.street:
mapView.setStreetView(true);
break;
case R.id.traffic:
mapView.setTraffic(true);
break;
case R.id.normal:
mapView.setSatellite(false);
mapView.setStreetView(false);
mapView.setTraffic(false);
break;
}
}
class InterestingLocations extends ItemizedOverlay {
private List<OverlayItem> locations = new ArrayList<OverlayItem>();
private Drawable marker;
public InterestingLocations(Drawable marker)
{
super(marker);
this.marker=marker;
//5.34079, 100.28241 is IICP's lat and long
GeoPoint npark = new GeoPoint((int)(5.34079*1000000),(int)(100.28241*1000000));
locations.add(new OverlayItem(npark , "IICP", "IICP"));
populate();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
protected OverlayItem createItem(int i) {
return locations.get(i);
}
@Override
public int size() {
return locations.size();
}
}
}


mapview.xml layout


 <?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/mapview.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/sat" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Satellite"
android:onClick="myClickHandler" android:padding="8px" />
<Button android:id="@+id/street" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Street"
android:onClick="myClickHandler" android:padding="8px" />
<Button android:id="@+id/traffic" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Traffic"
android:onClick="myClickHandler" android:padding="8px" />
<Button android:id="@+id/normal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Normal"
android:onClick="myClickHandler" android:padding="8px" />
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/mapview" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:clickable="true"
android:apiKey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
</LinearLayout>