带有可选图钉的Android Map

时间:2012-01-05 22:28:23

标签: android android-maps

有没有办法在Android地图上放一个图钉,当它被触摸时会显示一个带有一些额外信息的弹出窗口?

1 个答案:

答案 0 :(得分:0)

您需要扩展此http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/ItemizedOverlay.html

public class CustomOverlay extends ItemizedOverlay<OverlayItem> {

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

public CustomOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    this.context = context;

    //after adding things to the overlay, call these:
    setLastFocusedIndex(-1);
    populate();
}

@Override
protected boolean onTap(int index) {
    //called when an item is tapped
    return true;
}

@Override
public boolean onTap (final GeoPoint p, final MapView mapV) {
    boolean tapped = super.onTap(p, mapV);
    if(!tapped){            
        //you can use this to check for other taps on the custom elements you are drawing
    }                             
    return true;
}

@Override
public void draw(Canvas canvas, MapView mapV, boolean shadow){
    if(!shadow) 
    // if you have a custom image you may not want the shadow to be drawn
        super.draw(canvas,mapV,shadow);
    if(selected != null) { 
    // selected just means that something was clicked
    // it isn't defined in this example
    Projection projection = mapV.getProjection();
    Point drawPoint = projection.toPixels(selected.getPoint(), null);
        //get coordinates so you can do your drawing code afterward
    }
}

@Override
protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
}

@Override
public int size() {
    return mOverlays.size();
}
}

这是您需要做的非常粗略的草图。希望这会有所帮助。