1

我想在地图视图中移动标记。起初,我是这样的:

public void moveMarker(Marker marker, int lng, int lat) {       
    if (marker == null) {
        return;
    }
    String key = marker.getKey();
    OverlayItem item = (OverlayItem) markers.get(key);
    GeoPoint point = new GeoPoint(lat, lng);
    GeoPoint oldPoint = item.getPoint();
    if (oldPoint.getLatitudeE6() == point.getLatitudeE6()
            && oldPoint.getLongitudeE6() == point.getLongitudeE6()) {
        return;
    }
    overlay.removeOverlay(item);
    OverlayItem newItem = new OverlayItem(point, marker.getTitle(), marker
            .getSnippet());
    newItem.setMarker(marker.getIcon());
    overlay.addOverlay(newItem);
    mapView.postInvalidate();
    markers.put(key, newItem);

}

是的,这行得通。但是当一个标记移动时,其他标记会刷新。所以我修改如下代码:

public void moveMarker(Marker marker, int lng, int lat) {
    if (marker == null) {
        return;
    }
    String key = marker.getKey();
    MarkerOverlay overlay = markers.get(key);
    OverlayItem item = overlay.getItem(0);
    GeoPoint point = new GeoPoint(lat, lng);
    GeoPoint oldPoint = item.getPoint();
    if (oldPoint.getLatitudeE6() == point.getLatitudeE6()
            && oldPoint.getLongitudeE6() == point.getLongitudeE6()) {
        return;
    }
    mapOverlays.remove(overlay);
    MarkerOverlay overlayN = null;
    if (marker instanceof CarMarker) {
        CarMarker cm = (CarMarker) marker;
        overlayN = new CarMarkerOverlay(null, mapView, cm.getCarId());
    } else if (marker instanceof Marker) {
        overlayN = new MarkerOverlay(null);
    }
    OverlayItem newItem = new OverlayItem(point, marker.getTitle(),
            marker.getSnippet());
    newItem.setMarker(marker.getIcon());
    overlayN.addOverlay(newItem);
    mapOverlays.add(overlayN);
    markers.put(key, overlayN);
    mapView.postInvalidate();
}

这可以按我的意愿工作。但是有一个问题,每次marker移动的时候,都会去掉MarkerOverlay,重新创建,是不是效率低下?提前致谢!变量命名标记和 mapOverlays 定义:

private Map<String, MarkerOverlay> markers = new ConcurrentHashMap<String, MarkerOverlay>();
private List<Overlay> mapOverlays = null;
mapOverlays = mapView.getOverlays();

一些类定义:

public class MarkerOverlay extends ItemizedOverlay<OverlayItem>{}
public class CarMarkerOverlay extends MarkerOverlay {}
4

0 回答 0