Only display annotations within visible mapview area

358 views Asked by At

I've created a mapview with around 200 custom annotations. I'd like to redraw the annotations when they move into the visible mapview area and delete them when they leave (are scrolled out of the mapview.) Is this even possible? If so can someone please give me a clue as to how to get started?

1

There are 1 answers

0
cjnevin On

MKMapView has a property called 'visibleMapRect' which returns an MKMapRect, you can then use MKMapRectContainsPoint or MKMapRectContainsRect for each of the annotations you have added to the map.

You can see if a MKMapRect contains a point using:

MKMapRectContainsPoint(mapPoint)

Or a rect:

MKMapRectContainsRect(mapAnnotationRect)

You can get the MKMapPoint from a CLLocationCoordinate2D using:

CLLocationCoordinate2D mapCoordinate = ...;
MKMapPoint mapPoint = MKMapPointForCoordinate(mapCoordinate);