How to draw only the markers that are visible on the map?

898 views Asked by At

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

There are 1 answers

2
devunwired On

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.