开发者

Toggling Android MapView Overlays

开发者 https://www.devze.com 2023-02-02 17:56 出处:网络
I\'m trying to do the following, on a button click toggle the Overlays on and off. List<Overlay> savedOverlays;

I'm trying to do the following, on a button click toggle the Overlays on and off.

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


super.onCreate(savedInstanceState);

setContentView(R.layout.main);
MapV开发者_高级运维iew 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));
    }
};

Now the toggle off part works but it doesn't seem to want to bring them back, am I missing something?


The only thing I can think of, at the moment, is to call mapView.invalidate() after you have added all the overlays, to force a redraw.

0

精彩评论

暂无评论...
验证码 换一张
取 消