Android MapView can't remove marker

戏子无情 提交于 2019-12-01 08:47:53

according to Dave's suggestion this fixed the problem :

  public void onLocationChanged(Location location) {
    if(mMyOverlay == null) {
        mMyOverlay = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);
        MyMap.getOverlays().add(mMyOverlay);
    }else{
        MyMap.getOverlays().remove(mMyOverlay);
        MyMap.invalidate();
        mMyOverlay = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);
        MyMap.getOverlays().add(mMyOverlay);
    }
    if(location!=null){
        MyMap.invalidate();
        GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()),microdegrees(location.getLongitude()));
        MyController.animateTo(MyPos);
        mMyOverlay.addPoint(MyPos,"Ma position","Ma position");
     }

thanks..

getOverlays() returns the List of Overlay objects in your MapView. Each time the location is updated, your code is adding another overlay to this list. You need to do is remove the previous overlay, or empty the list entirely each time.

So, in your method call getOverlays().clear() before adding your new overlay.

Edit, alternatively maybe this will point you in the right direction? I don't know exactly how your myOverlay class works, so hopefully you can fill in the blanks!

protected myOverlay mMyOverlay; 

public void onLocationChanged(Location location) {
  if(mMyOverlay == null) {
      mMyOverlay = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);
      MyMap.getOverlays().add(mMyOverlay);

  if(location!=null){
      MyMap.invalidate();
      GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()),microdegrees(location.getLongitude()));
      MyController.animateTo(MyPos);
      mMyOverlays.setPoint(MyPos,"Ma position","Ma position");
   }
}

Use an ItemizedOverlay, and as Dave suggested, keep track of the one you want moved and then either delete the tracked OverlayItem and add a new one or take the tracked one, get its marker or MapPoint and reset its lat/lon coordinates to the new location coordinates. You may also have to call mapView.invalidate() to force a redraw.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!