Search code examples
androidgoogle-mapsitemizedoverlayindexoutofboundsexception

Removing an OverylayItem from Map


I am trying to allow a User to remove a point (or more specifically, an OverlayItem) from a map. I followed the developer tutorial to get started and implemented the CustomMapView in this tutorial to capture a long press on the map.

So now I have a program which allows a User to place points on the map. My next goal is to let the User remove points. Here is my code for when a User clicks an existing point on the map.

public class OurItemizedOverlay extends ItemizedOverlay {

//Create new list of points
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context mapContext;
@Override
protected boolean onTap(final int index) {
    Button edit, remove;

    //Get index of item tapped
    OverlayItem item = mapOverlays.get(index);

    //Create Dialog to show point info, allow for edit or removal.
    LinearLayout layout = new LinearLayout(mapContext);
    layout.setOrientation(LinearLayout.VERTICAL);

    LayoutInflater inflater = LayoutInflater.from(mapContext);
    AlertDialog.Builder builder = new AlertDialog.Builder(mapContext);
    builder.setTitle(item.getTitle());
    builder.setMessage(item.getSnippet());

    View view = inflater.inflate(R.layout.view_or_edit_location_dialog, null);
    builder.setView(view); 
    builder.show();

    //BUTTONS

    edit = (Button)view.findViewById(R.id.edit);
    remove = (Button)view.findViewById(R.id.delete);

    //Edit Button Listener
    edit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });

    //Remove Button Listener
    remove.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            removeOverlay(index); <--------

            Log.d("View location info", "user clicked delete.");
            return;
        }
    });

    return true;
}

Here is my code for removeOverlay.

protected void removeOverlay(int index) {

    mapOverlays.remove(index);
    com.example.mapproject.MainActivity.mapView.invalidate();
}

After I click on an existing point a dialog is presented offering to remove the point. When I have selected to remove the point, the point still remains on the screen. If I place a new point, the 'deleted' one is removed. However, if I click the 'deleted' point or another existing point, the program crashes with this error.

error

If you have a clue of what to do, I'd appreciate to hear from you !!

Edit

Following a tip from Vishwa Patel, I remove a point from the map straight away using postInvalidate(). However, I still get indexoutofbounds exceptions when I click where the icon was..


Solution

  • I believe I found the solution here. It seems to work so far, the answer was to put the following line into my removeOverlay method,

    setLastFocusedIndex(-1);
    

    The code to remove an OverylayItem from my custom Overlay is,

        protected void removeOverlay(OverlayItem overlayItem) {
    
        mapOverlays.remove(overlayItem);
        MainActivity.mapOverlays.remove(this);
        setLastFocusedIndex(-1);
        populate();
    
    }
    

    Any thoughts/suggestions are welcome!