如何在Android地图上更改位置时删除标记并重新重绘

时间:2012-04-21 10:40:45

标签: android map location marker

////编辑之前

我使用下面的代码在android地图上重绘标记,实际上它重绘了一个标记,但它没有删除旧标记,我试过

mapView.invlaidate

但它没有删除旧的。

这是onLocationChanged函数:

   @Override
 public void onLocationChanged(Location location) {
    // mapView.invalidate();

        //getCurrantPoint();
         OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");

            itemizedoverlay2.addOverlay(currentOverlay);
            mapOverlays.add(itemizedoverlay2);
            mapOverlays.remove(currentOverlay);
            Log.v("TAG", "Removeeeeeeeeeeeeeeeed");

         ///
         //mapOverlays.add(itemizedoverlay);
 }

先谢谢。

/////编辑后

 if(itemizedoverlay2!=null)
     {
         mapOverlays.remove(itemizedoverlay2);

         itemizedoverlay2 = new HelloItemizedOverlay(drawable2, cntxt);
         OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
         itemizedoverlay2.addOverlay(currentOverlay);

         mapOverlays.add(itemizedoverlay2);
     }
     else
     {
            itemizedoverlay2 = new HelloItemizedOverlay(drawable2, cntxt);

            OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
            itemizedoverlay2.addOverlay(currentOverlay);
            mapOverlays.add(itemizedoverlay2);

     }

非常感谢imran khan和agarwal;我用你的答案来调试问题。

3 个答案:

答案 0 :(得分:3)

试试这个:

OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
itemizedoverlay2.addOverlayItem(currentOverlay);
mapOverlays.getOverlays().add(itemizedoverlay2);
mapOverlays.getOverlays().remove(itemizedoverlay2);
mapOverlays.invalidate();
Log.v("TAG", "Removeeeeeeeeeeeeeeeed");

// mapOverlays是你的mapView对象,itemized overlay2是你的LocationOverlay

删除LocationOverlay,即逐项列出的overlay2而不是OverlayItem

答案 1 :(得分:1)

试试这个:::

@覆盖  public void onLocationChanged(Location location){     // mapView.invalidate();

    //getCurrantPoint();
     OverlayItem currentOverlay = new OverlayItem(getCurrantPoint(),"Current Location","Here is my current location!!!");
        itemizedoverlay2.clear();
        itemizedoverlay2.addOverlay(currentOverlay);
       //in above line create new itemizedoverlay2 every time.
        mapOverlays.clear();
        mapOverlays.add(itemizedoverlay2);            

     ///
     //mapOverlays.add(itemizedoverlay);

}

更新后看看:::

典型的自定义叠加层如下所示。它封装了列表中地图上显示的各种OverlayItem。

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{

    private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    public MyItemizedOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));        
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    public void removeOverlay(OverlayItem overlay) {
        mOverlays.remove(overlay);
        populate();
    }


    public void clear() {
        mOverlays.clear();
        populate();
    }

    @Override
    public int size() {
        return mOverlays.size();
    }
}

可以公开方法来添加/删除单个叠加项,还可以删除所有叠加项(清除方法)。

删除单个叠加层

MyItemizedOverlay sitesOverlay =  (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.removeOverlay(overlay);

添加一个overlayItem

MyItemizedOverlay sitesOverlay =  (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.addOverlay(new OverlayItem(p, "title", "snippet"));

删除所有overlayItems

MyItemizedOverlay sitesOverlay =  (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.clear();

答案 2 :(得分:1)

为什么重新发明自己的距离计算器,Location类内置了一个。

结帐

distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) 

计算两个位置之间的近似距离,以及它们之间最短路径的初始和最终方位。