写一个类继承ItemizedOverlay
package cn.fn;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlayItem) {
mapOverlays.add(overlayItem);
this.populate();
}
}
MainActivity 取MapView 进行设置
package cn.fn;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class TextActivity extends MapActivity {
private MapView mapView;
private static final int lat1 = 39984881;
private static final int lon1 = 116337044;
private static final int lat2 = 39977293;
private static final int lon2 = 116353089;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.myMapView);
mapView.setStreetView(true);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.hua);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
itemizedOverlay.addOverlay(new OverlayItem(new GeoPoint(lat1, lon1), "HaHa", "HaHa"));
itemizedOverlay.addOverlay(new OverlayItem(new GeoPoint(lat2, lon2), "HaHa", "HaHa"));
mapOverlays.add(itemizedOverlay);
MapController mapController = mapView.getController();
//定位到显示的位置
mapController.animateTo(new GeoPoint(locallat, locallon));
mapController.setZoom(15);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
[color=red]提示:这个现在还有一个问题就是加太多的话就会[/color]
package cn.fn;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlayItem) {
mapOverlays.add(overlayItem);
this.populate();
}
}
MainActivity 取MapView 进行设置
package cn.fn;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class TextActivity extends MapActivity {
private MapView mapView;
private static final int lat1 = 39984881;
private static final int lon1 = 116337044;
private static final int lat2 = 39977293;
private static final int lon2 = 116353089;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.myMapView);
mapView.setStreetView(true);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.hua);
CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);
itemizedOverlay.addOverlay(new OverlayItem(new GeoPoint(lat1, lon1), "HaHa", "HaHa"));
itemizedOverlay.addOverlay(new OverlayItem(new GeoPoint(lat2, lon2), "HaHa", "HaHa"));
mapOverlays.add(itemizedOverlay);
MapController mapController = mapView.getController();
//定位到显示的位置
mapController.animateTo(new GeoPoint(locallat, locallon));
mapController.setZoom(15);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
[color=red]提示:这个现在还有一个问题就是加太多的话就会[/color]