切换Android MapView叠加层

时间:2011-01-02 22:28:52

标签: android android-maps

我正在尝试执行以下操作,在按钮上单击以打开和关闭叠加层。

List<Overlay> savedOverlays;
Button testButton;
@Override
public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
testButton = (Button) findViewById(R.id.TestButton);
testButton.setOnClickListener(testButtonClickListener);
mapView.setBuiltInZoomControls(true);
setOverlays(mapView);
}
private void setOverlays(MapView mapView) {
    List<Overlay> mapOverlays = getOverlays(mapView);
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
GeoPoint point2 = new GeoPoint(35410000, 139460000);
OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
itemizedoverlay.addOverlay(overlayitem);
itemizedoverlay.addOverlay(overlayitem2);
mapOverlays.add(itemizedoverlay);

}

private List<Overlay> getOverlays(MapView mapView) {
    List<Overlay> mapOverlays = mapView.getOverlays();
    return mapOverlays;
}

private void toggleOverlays(MapView mapView){
    List<Overlay> mapOverlays = mapView.getOverlays();
    if (mapOverlays.isEmpty())
    {
        setOverlays(mapView);
    }
    else
    {
        mapOverlays.removeAll(getOverlays(mapView));
    }
}

private OnClickListener testButtonClickListener = new OnClickListener() {
    public void onClick(View V){
        toggleOverlays((MapView) findViewById(R.id.mapview));
    }
};

现在切换部分有效,但它似乎不想把它们带回来,我错过了什么?

1 个答案:

答案 0 :(得分:3)

目前,我唯一想到的是在添加所有叠加层后调用mapView.invalidate()来强制重绘。