2013-08-24 96 views
1

我用自定义标记标记当前用户的位置。我画了一次,并在OnLocationChange听众我根据用户位置更改它的位置,但有时在地图上有一个重复的标记。为什么?你有什么想法?在OnLocationChange听众中更改标记位置是否好主意?在Android Google Maps API v2中标记用户位置

我绘制标记:

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()删除标记是可以接受的,但在我有很多标记和例如在地图上绘制的多段线是不好的主意。

如果地图有一个单独的删除标记和折线的方法,但现在有一个方法可以清除所有内容,这将非常有用。

+0

当你改变位置,然后删除前一个标记.. – Piyush

+0

是的,我试过了,但不知何故,我失去了绘制标记的参考。 –

+0

当你改变你的位置,然后尝试删除以前,并使用新的位置创建一个新的标记... – Piyush

回答

0

根据你的描述,我只能说它只能在currentUserLocationMarker在函数调用之间变为空时发生。尝试调试并使用logcat。

相关问题