我想在地图视图中移动标记。起初,我喜欢这样:
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();
}
这是我想要的。但是有一个问题,每当标记移动时,它会删除MarkerOverlay并重新创建它,它是否效率低下?提前致谢! 名为markers和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 {}