【发布时间】:2013-08-27 10:55:05
【问题描述】:
我通过自定义标记标记当前用户位置。我画了一次,然后在OnLocationChange 监听器中根据用户位置更改它的位置,但有时地图上会有重复的标记。为什么?你有什么想法?更改OnLocationChange 侦听器中的标记位置是个好主意吗?
我画标记:
currentUserLocationMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(currentUserLocation.latitude, currentUserLocation.longitude))
.title("Current Location"));
并改变他的位置:
currentUserLocationMarker.setPosition(locLatLng);
我的方法如下所示。我在onCreateView() 和OnMyLocationChangeListener() 中调用过一次。
if (currentUserLocationMarker == null) {
currentUserLocationMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(currentUserLocation.latitude, currentUserLocation.longitude))
.title("Current Location"));
} else {
currentUserLocationMarker.setPosition(locLatLng);
}
我认为更好的解决方案是绘制一次,然后改变它的位置,而不是一直绘制、清除和绘制。
通过调用方法.remove() 删除标记是可以接受的,但是清除整个地图,如果我有很多标记,例如在地图上绘制折线是个坏主意。
如果地图有单独的删除标记和折线的方法会很有用,但现在有一个方法.clear() 可以清除所有内容。
【问题讨论】:
-
当您更改位置时,然后删除前一个标记..
-
是的,我试过了,但不知何故我丢失了绘制标记的参考。
-
当你改变你的位置然后尝试删除以前的并用新的位置制作一个新的标记......
-
sometimes there is a duplicate markers on the map不能很好地描述问题。特别是请记下addMarker函数在哪里执行。 -
我在上面写过我试过了,我保存了对字段的标记引用,并且对于每个更改位置,我都删除了以前的标记并再次绘制新的标记,但是碰巧我在地图上有两个标记,直到我使用
.clear()方法清除了整个地图。
标签: java android google-maps google-maps-markers