[英]Marker on user location in Android Google Maps API v2
我通过自定义标记标记当前用户位置。 我绘制了一次,然后在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()
可以清除所有内容。
根据您的描述,我只能说只有在函数调用之间currentUserLocationMarker变为null时才会发生。 尝试调试并使用logcat。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.