[英]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.