-1

以前に作成したすべての 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);
    }
}
}
4

1 に答える 1

0

mapView.postInvalidate()の代わりに試してくださいmapView.invalidate()ドキュメント> MapView > getOverlays()を参照してください。

于 2012-11-07T15:44:04.260 に答える