0

I have a map with lots of points on it added to an ItemizedOverlay.

OverlayItem overlayItem = new OverlayItem(theGeoPoint, title, description);
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);

Is there a way to remove a specific point from the itemizedOverlay?

Example, say I've added lots of point at different latitudes/longitudes and I wish to remove a point at latitude: 32.3121212 and longitude: 33.1230912, which was added earlier.

How can I remove JUST that point??

I really need this so I hope someone can help.

Thanks.

Full story scenario (in case you have a different idea on how to solve this): Adding events to a map that are caught from a database. Now when events are deleted from the database, I wish to sync the map and remove just those which were deleted. (please don't suggest I re-download all points excluding the deleted ones even though I've thought of that but it isn't an option concerning what I want to do. :p)

bubbly
  • 495
  • 1
  • 10
  • 22

1 Answers1

4

Create your MapOverlay with GeoPoints Array and override the draw function:

public class MapOverlay extends Overlay 
{

    private ArrayList<GeoPoints>points;
    ...


    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
    {
            super.draw(canvas, mapView, shadow);      
            int len = points.size();  
            if(len > 0)
            {
                for(int i = 0; i < len; i++)
                {
                   // do with your points whatever you want
                   // you connect them, draw a bitmap over them  and etc.
                   // for example:
                   Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.pointer);
                   mapView.getProjection().toPixels(points.get(i), screenPts);
                   canvas.drawBitmap(bmp, screenPts.x-bmp.getWidth()/2, screenPts.y - bmp.getHeight()/2, null);  
                } 
            }
    }

    public void addPoint(GeoPoint p)
    {
       // add point to the display array
    }

    public void removePointByIndex(int i)
    {
       points.remove(i);
    }

    public void removePointByCordinate(Double lat, Double lng)
    {
        int index = -1;
        int len = points.size();  
        if(len > 0)
        {
                for(int i = 0; i < len; i++)
                {
                     if((int)(lat*1E6) == points.get(i).getLatitudeE6() && (int)(lng*1E6) == points.get(i).getLongitudeE6())
                     {
                          index = i;
                     }
                } 
            }

            if(index != -1)
            {
                points.remove(index);
            }
        }
    }

    public void removePoint(GeoPoint p)
    {
        int index = -1;
        int len = points.size();  
        if(len > 0)
        {
                for(int i = 0; i < len; i++)
                {
                     if(p == points.get(i))
                     {
                          index = i;
                     }
                } 
            }

            if(index != -1)
            {
                points.remove(index);
            }
        }
    }

}

(I didn't test above class)

and then in your MapActivity class you can just:

MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setClickable(true);
MapOverlay mapOverlay = new MapOverlay();                       
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.add(mapOverlay);

Try google some GoogleMap tutorials and maybe you will find more solutions.

goodm
  • 7,275
  • 6
  • 31
  • 55
  • Thank you for the great effort you put in answering me. Nice code you gave there. I can understand a great deal of it but I do not see the part where I can actually REMOVE a specific point. method removePoint(GeoPoint p), what is supposed to go under there? That is what's troubling me. Also, at the part where you mention MapActivity class, won't those commands clear all points from the listOfOverlays? – bubbly May 17 '12 at 21:20
  • this method you call only once at the beginning, I copy it from my code. You don't need to clear before. – goodm May 17 '12 at 21:21
  • The removePoint(GeoPoint p) simply removes a point from the array right? But how does it know which point I'm talking about if I send in coordinates? (geopoint) – bubbly May 18 '12 at 08:04
  • 1
    I improve my code, but I'm not sure is it gonna work, didn't tested. – goodm May 18 '12 at 08:35
  • I managed to do something with the idea you gave me but I got some errors. Please take a look at my other question see if you can help out :) http://stackoverflow.com/questions/10653938/adding-custom-index-to-arraylist-or-sortedmap-when-adding-overlayitem-to-itemize – bubbly May 18 '12 at 15:39