简体   繁体   English

Android Google Maps API v2中的用户位置标记

[英]Marker on user location in Android Google Maps API v2

I mark current user location by a custom marker. 我通过自定义标记标记当前用户位置。 I draw it once and in OnLocationChange listener I change it position according to user location, but sometimes there is a duplicate markers on the map. 我绘制了一次,然后在OnLocationChange侦听器中根据用户位置更改了它的位置,但是有时在地图上有重复的标记。 Why? 为什么? Do you have any ideas? 你有什么想法? And is it good idea to change marker position in OnLocationChange listener? OnLocationChange侦听器中更改标记位置是个好主意吗?

I drawing marker: 我画标记:

currentUserLocationMarker = mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(currentUserLocation.latitude, currentUserLocation.longitude))
                    .title("Current Location"));

and changing his position: 并更改他的位置:

currentUserLocationMarker.setPosition(locLatLng);

My method looks like below. 我的方法如下所示。 I invoke it once in onCreateView() and in OnMyLocationChangeListener() . 我在onCreateView()OnMyLocationChangeListener()调用一次。

if (currentUserLocationMarker == null) {
            currentUserLocationMarker = mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(currentUserLocation.latitude, currentUserLocation.longitude))
                    .title("Current Location"));
        } else {
            currentUserLocationMarker.setPosition(locLatLng);
        }

I suppose that better solution is draw once, and changing it position instead drawing, clearing and drawing again all the time. 我想更好的解决方案是绘制一次,然后一直更改它的位置,而不是绘制,清除和重新绘制。

Removing marker by calling method .remove() is acceptable, but clearing whole map, in the case of I have many markers and eg polylines drawn on the map is bad idea. 可以通过调用方法.remove()删除标记,但是清除整个地图,在我有很多标记的情况下,例如在地图上绘制折线是个坏主意。

It would be useful if the map had a separate methods for remove markers and polylines, but now there is a one method .clear() which clear everything. 如果地图具有用于删除标记和折线的单独方法,这将很有用,但是现在只有一种方法.clear()可以清除所有内容。

According to your description I can only say that it can only happen if currentUserLocationMarker becomes null between function call. 根据您的描述,我只能说只有在函数调用之间currentUserLocationMarker变为null时才会发生。 try debugging and using logcat. 尝试调试并使用logcat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM