开发者

Cant remove overlay from map

Working on a MapView app with Itemized overlays where i load a bunch of markers. This part works well, but im also adding a marker/overlay where you tap on the map.. that also works, but i want the last marker to be removed when i tap the map again but that seems impossible.

For ex i load some markers like this

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);

And when i tap on the map i add a marker like this

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);

But the overlay "itemwarning" dont gets removed when i tap the map and ad new ones I searched and tried to fix this for days but i cant figure it out...

mapView.getOverlays().remove(0); Clears everything, but i dont want that, i only wanna clear the ones called "itemwarning"

any ideas what im missing ?

Here is all the code for the overlays.

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");
    itemWarnin开发者_Go百科g.addOverlayItem(witemWarning);
    mapView.getOverlays().add(itemWarning);

        }

    }</code>


I think you should call mapView.invalidate(); after

  mapView.getOverlays().remove(itemWarning);

The idea is to redraw after you have remove the overlay elements

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜