删除Google地图中的所有MapOverlay(Android 2.2)

时间:2012-11-07 15:37:32

标签: android google-maps

我想删除之前创建的所有MapOverlay。 不幸的是我的代码不起作用。 感谢。

mapOverlays = mapView.getOverlays();
mapOverlays.clear();

mapView.invalidate();

编辑: 现在我用mapView.postInvalidate()修正了它; 出现同样的问题。 这是我的整个代码:

public class GMapsSubActivity extends MapActivity {

Button b1;
TextView t1, t2;
MapView mapView;
Drawable marker;
GMapsItemizedOverlay itemizedoverlay, markerOverlay;
List<Overlay> mapOverlays;
MapController mc;
int i;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gmaps_sub);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    GeoPoint htl = new GeoPoint(47821995, 13046345);

    mc = mapView.getController();
    mc.setCenter(htl);
    mc.setZoom(17);

    marker = this.getResources().getDrawable(R.drawable.marker);
    markerOverlay = new GMapsItemizedOverlay(marker, this);

    this.b1 = (Button) findViewById(R.id.gmapssub_button1);
    this.t1 = (TextView) findViewById(R.id.gmapssub_textView1);
    this.t2 = (TextView) findViewById(R.id.gmapssub_textView2);

    // onClickListener zuweisen
    this.b1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            toggleSatellite();
        }
    });

    i = 8;

    // LocationManager
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new LocationListener() {

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if (location != null) {
                double pLat = location.getLatitude();
                double pLong = location.getLongitude();

                t1.setText(Double.toString(pLat));
                t2.setText(Double.toString(pLong));

                if(i == 8) {
                    i = 0;
                    mapOverlays = mapView.getOverlays();
                    mapOverlays.clear();

                    GeoPoint myPos = new GeoPoint((int) (pLat * 1E6), (int) (pLong * 1E6));
                    OverlayItem overlayitem = new OverlayItem(myPos, "Your current Location", "You are here!");
                    markerOverlay.addOverlay(overlayitem);
                    mapOverlays.add(markerOverlay);

                    mapView.postInvalidate();

                    mc.animateTo(myPos);
                } else {
                    i++;
                }
            }
        }
    };

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_gmaps_sub, menu);
    return true;
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

private void toggleSatellite() {
    if (mapView.isSatellite() == true) {
        mapView.setSatellite(false);
    } else {
        mapView.setSatellite(true);
    }
}
}

1 个答案:

答案 0 :(得分:0)

尝试使用mapView.postInvalidate()代替mapView.invalidate()。请参阅documentation&gt; MapView&gt; getOverlays()。