Load annotations for visible region in MKMapView

落花浮王杯 提交于 2019-12-20 08:52:28

问题


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?


回答1:


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...




回答2:


MKMapRect visibleMapRect = mapView.visibleMapRect;
NSSet *visibleAnnotations = [mapView annotationsInMapRect:visibleMapRect];



回答3:


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



来源:https://stackoverflow.com/questions/4126364/load-annotations-for-visible-region-in-mkmapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!