0

每当当前位置发生变化时,我都会显示marker在我的身上。current location下面的代码工作正常。但问题是,-假设我第一次经过某个位置,我的标记将在那个位置,但第二次如果我经过任何其他不同的位置,那么我的标记将在第二个位置但在同一位置时间我的标记也停留在第一个位置,我不想要这个东西。我只需要在当前位置发生变化时,所有其他标记都应该消失,只有当前位置标记应该在那里。希望我对每个人都清楚

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        mapController = mapView.getController();
        mapController.setZoom(15);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, new GeoUpdateHandler());

    }

    public class GeoUpdateHandler implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            int lat = (int) (location.getLatitude() * 1E6);
            int lng = (int) (location.getLongitude() * 1E6);

            Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
            int markerWidth = marker.getIntrinsicWidth();
            int markerHeight = marker.getIntrinsicHeight();
            marker.setBounds(0, markerHeight, markerWidth, 0);


            myItemizedOverlay = new MyItemizedOverlay(marker);
            mapView.getOverlays().add(myItemizedOverlay);


            GeoPoint point = new GeoPoint(lat, lng);

            myItemizedOverlay.addItem(point, "myPoint1", "myPoint1");
            mapController.animateTo(point);
            String address = ConvertPointToLocation(point);
            Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

            mapView.invalidate();

        }
}

下面是我的 ItemizedOverlay 类-

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{

    private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();

    public MyItemizedOverlay(Drawable marker) {
        super(boundCenterBottom(marker));
        // TODO Auto-generated constructor stub

        populate();
    }

    public void addItem(GeoPoint p, String title, String snippet){
        OverlayItem newItem = new OverlayItem(p, title, snippet);
        overlayItemList.add(newItem);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return overlayItemList.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return overlayItemList.size();
    }

    public void clear() {
        overlayItemList.clear();
        populate();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        // TODO Auto-generated method stub
        super.draw(canvas, mapView, shadow);
        //boundCenterBottom(marker);
    }

}
4

1 回答 1

1

您只需在添加新项目之前删除以前添加的项目

public void addItem(GeoPoint p, String title, String snippet){
    OverlayItem newItem = new OverlayItem(p, title, snippet);
    overlayItemList.removeAll();
    overlayItemList.add(newItem);
    populate();
}
于 2012-08-12T15:14:05.340 回答