2

////编辑前

我正在使用下面的代码在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;我用你的答案来调试问题。

4

3 回答 3

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 对象,而 itemizedoverlay2 是您的 LocationOverlay

删除 LocationOverlay 即 itemizedoverlay2 而不是 OverlayItem

于 2012-04-21T11:01:29.127 回答
1

尝试这个:::

@Override 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"));

删除所有覆盖项

MyItemizedOverlay sitesOverlay =  (MyItemizedOverlay ) map.getOverlays().get(0);
sitesOverlay.clear();
于 2012-04-21T10:46:26.423 回答
1

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

查看

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

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

于 2012-04-21T13:29:04.360 回答