我通过自定义标记标记当前用户位置。我绘制了一次,并在 OnLocationChange 监听器中根据用户位置更改它的位置,但有时 map 上有重复的标记。为什么?你有什么想法?在 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()
删除标记是可以接受的,但是会清除整个 map ,如果我有很多标记,例如在 map 上绘制折线是个坏主意。
如果 map 有一个单独的方法来删除标记和折线,那将会很有用,但现在有一个方法 .clear()
可以清除所有内容。
最佳答案
根据你的描述,我只能说只有在函数调用之间 currentUserLocationMarker 变为 null 时才会发生这种情况。尝试调试并使用 logcat。
关于java - Android Google Maps API v2 中的用户位置标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18418921/