3

I want to learn drawing only the overlayitems that are visible on the map, because i'm showing a map with thousands of markers. I know that i must check if the overlayitem is visible with something like this:

private boolean isLocationVisible(location)
    {
        Rect currentMapBoundsRect = new Rect();
        Point currentDevicePosition = new Point();
        GeoPoint deviceLocation = new GeoPoint((int) (location.getLatitude() * 1000000.0), (int) (location.getLongitude() * 1000000.0));

        mapView.getProjection().toPixels(deviceLocation, currentDevicePosition);
        mapView.getDrawingRect(currentMapBoundsRect);

        return currentMapBoundsRect.contains(currentDevicePosition.x, currentDevicePosition.y);

    }

and then if this method returns true i must draw the overlayitem, and if not, i must not draw it.

The problem is that i can't override onDraw in OverlayItem, so i didn't know how to achieve my needs.

What should i change in my code to draw only the markers that are visible on the map?

This is my ItemizedOverlayClass:

            public class mItemizedOverlay extends ItemizedOverlay<OverlayItem> {
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    boolean onTapActivated=false; 
    private Drawable marker = null;
    private BusMap map = null;
    boolean comingFromFavoriteNameEdit=false; 

    public BmItemizedOverlay(Drawable defaultMarker, BusMap map) {
        super(boundCenterBottom(defaultMarker));
        this.map=map;
        this.marker=defaultMarker;
    }
    public mItemizedOverlay(Drawable defaultMarker, BusMap map, boolean onTapActivated) { 
          super(boundCenterBottom(defaultMarker));
          this.map=map;
          this.onTapActivated=onTapActivated;    
          this.marker=defaultMarker;
    }

    protected OverlayItem createItem(int i){return mOverlays.get(i);}

    public int size() {return mOverlays.size();}    

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

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
    }
    public void setOverlays(ArrayList <OverlayItem> overlays) {
        mOverlays=overlays;
        populate();
    }   

    public void draw(Canvas canvas, MapView mapView, boolean shadow){
        if (mapView.getZoomLevel() > 17){
            boundCenterBottom(this.marker);
            super.draw(canvas, mapView, false);         
        }
    }
}
Pableras84
  • 1,195
  • 5
  • 18
  • 30
  • I've used the itemized overlay with hundreds of items and it works smoothly. How many items are you adding to the itmenized overlay? – Luis Oct 31 '12 at 17:43

1 Answers1

1

You should not have to handle this yourself. It is the job of ItemizedOverlay to only dispatch and draw the markers that are currently present inside the bounds of the visible map. What is leading you to believe this is not already occurring?

If you decide that the current Overlay implementation is not satisfactory, you will likely have better luck using your checking method to determine when to add or remove items from the Overlay, rather than attempting to override the draw calls.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • 1
    i'm absolutly sure that i'm following the good path. If you search on google you will see a lot of guys with this problem and a lot of guys telling that the solution is to draw only the visible markers manually. The problem is that none of them have been published the code for doing it – Pableras84 Oct 30 '12 at 19:03
  • Then as I mentioned, the best case would be to add/remove items from the `ItemizedOverlay` based on the result of your method check. You would need to add a method so you can remove an `OverlayItem` as well, and I think `populate()` needs to be called each time you change the content (don't quote me on that one). – devunwired Oct 30 '12 at 20:21