I have about 400 MKAnnotationView´s that loads simultaneously into the MKMapView.
I understand that this isn't any good, it is a little bit slow, and I want to do it the "correct" way.
I zoom my map by a center coordinate:
MKCoordinateSpan span;
span.latitudeDelta = 0.8;
span.longitudeDelta = 0.8;
MKCoordinateRegion region;
region.span = span;
region.center = self.selectedCounty.coordinate;
[mapView setRegion:region animated:TRUE];
I only want to load the annotations that could be visible in that开发者_运维知识库 region.
I have a custom MKAnnotation called simply "Annotation" with a CLLocationCoordinate2D- and title-property.
I simply want to load the annotation for the "visible area" on the MKMapView so not all the annotation loads at the same time. And when the "visible area" on the MKMapView changes, I of course want to load annotations for that area.
I know that MKMapView has a delegate method which runs when the region changes.
But how do I know what annotations I should load for that region?
MKMapRect visibleMapRect = mapView.visibleMapRect;
NSSet *visibleAnnotations = [mapView annotationsInMapRect:visibleMapRect];
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKitFunctionsReference/Reference/reference.html
MKMapRectContainsPoint will tell you if it is visible.
visibleCount = 0;
for (MyAnnotation *annotation in mapView.annotations) {
if (MKMapRectContainsPoint(mapView.visibleMapRect, MKMapPointForCoordinate(annotation.coordinate)))
{
visibleCount++;
}
}
I recommend doing this method on a background thread if you have a lot of annotations. but you can determine if it is visible in the map
You can get the map region span and center, and based on the locations of the annotations you could check if any annotation is inside that region... maybe it is implemented already in something like [mapview isAnnotationVisible]... but you'll have to check everyone of the annotations eventually...
精彩评论