无法从地图中删除叠加层

时间:2011-07-05 10:05:00

标签: android

使用Itemized覆盖图处理MapView应用程序,其中我加载了一堆标记。 这部分效果很好,但我也添加了一个标记/叠加,你点击地图.. 这也有效,但我希望当我再次点击地图时删除最后一个标记,但这似乎是不可能的。

对于ex我加载一些像这样的标记

OverlayItem itemAnnat = new OverlayItem(point, sitesList.getName().get(i), sitesList.getName().get(i));
            Drawable marker4 = getResources().getDrawable(R.drawable.annat);
            marker4.setBounds(0, 0, getResources().getDrawable(R.drawable.annat).getIntrinsicWidth(), getResources().getDrawable(R.drawable.annat).getIntrinsicHeight());
            itemAnnat.setMarker(marker4);
            locationOverlay.addOverlayItem(itemAnnat);

当我点击地图时,我会添加一个这样的标记

mapView.invalidate();

mapView.getOverlays().remove(itemWarning);

GeoPoint newpoint = new GeoPoint(markerLatitude, markerLongitude);
OverlayItem witemWarning = new OverlayItem(newpoint, "add rub", "desc"); itemWarning.addOverlayItem(witemWarning); mapView.getOverlays().add(itemWarning);

但是,当我点按地图并添加新地图时,叠加“项目警告”不会被删除 我搜索并试图解决这个问题好几天,但我无法弄明白......

调用MapView.getOverlays()除去(0); 清除一切,但我不想要那个,我只想清除那些叫做“项目警告”

我缺少什么想法?

以下是叠加层的所有代码。

public class LocationOverlay extends ItemizedOverlay {

        private ArrayList<OverlayItem> overLayList = new ArrayList<OverlayItem>();
        private MapView mapView;


        public LocationOverlay(MapView mapView, Drawable defaultMarker) {
             super(boundCenterBottom(defaultMarker));
             setLastFocusedIndex(-1);
             populate();
             this.mapView = mapView; // need it for onTap
        }

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

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

        public void addOverlayItem(OverlayItem overlayItem) {
             if(!overLayList.contains(overlayItem)){
                  overLayList.add(overlayItem);
             }
             setLastFocusedIndex(-1);
             populate();
        }

        public void clear(){
             overLayList.clear();
        }  

        @Override
            public boolean onTap(int pIndex) {
            OverlayItem item = overLayList.get(pIndex);
            item.getTitle();
                    mapView.getController().animateTo(item.getPoint());
                    Toast.makeText(mapView.getContext(), item.getTitle(), Toast.LENGTH_SHORT).show();      
              return false;
            }
        public void draw( Canvas c, MapView m, boolean shadow ) {
            super.draw( c, m, false );
            }



        public boolean onTouchEvent(MotionEvent arg0, MapView arg1) {
            // TODO onTouchEvent

            int Action = arg0.getAction();
            if (Action == MotionEvent.ACTION_UP){

             if(!MoveMap)
             {
              Projection proj = mapView.getProjection(); 
              GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY());
              CenterLocation(loc);
             }


            }
            else if (Action == MotionEvent.ACTION_DOWN){

             MoveMap = false;

            }
            else if (Action == MotionEvent.ACTION_MOVE){    
             MoveMap = true;
            }
            //return MoveMap;
            return super.onTouchEvent(arg0, arg1);
            //return false;
           }

      //CenterLocation(loc);

        private void CenterLocation(GeoPoint centerGeoPoint)
        {

            mapController.animateTo(centerGeoPoint);

             myLongitude.setText("Longitude: "+
                       String.valueOf((float)centerGeoPoint.getLongitudeE6()/1E6));
                      myLatitude.setText("Latitude: "+
                       String.valueOf((float)centerGeoPoint.getLatitudeE6()/1E6));
                //    Toast.makeText(HelloMapView.this, "yeeey "+centerGeoPoint.getLatitudeE6(), 12).show();
         placeMarker(centerGeoPoint.getLatitudeE6(), 
           centerGeoPoint.getLongitudeE6());

        };

    private void placeMarker(int markerLatitude, int markerLongitude){
    LocationOverlay itemWarning = new LocationOverlay(mapView, getResources().getDrawable(R.drawable.marker));

    mapView.invalidate();
    mapView.getOverlays().remove(itemWarning);

    GeoPoint newpoint = new GeoPoint(markerLatitude, markerLongitude);  
    OverlayItem witemWarning = new OverlayItem(newpoint, "add rub", "desc");
    itemWarning.addOverlayItem(witemWarning);
    mapView.getOverlays().add(itemWarning);

        }

    }</code>

1 个答案:

答案 0 :(得分:0)

我认为您应该在

之后致电mapView.invalidate();
  mapView.getOverlays().remove(itemWarning);

想法是在删除叠加元素后重绘

相关问题