Handling large quantity of MKMapView Annotations

后端 未结 3 1567
刺人心
刺人心 2021-02-01 10:10

I have a map view with a large quantity of annotations (3000+), when the user is zoomed to a reasonable level all is well and fast.

Although when the user zooms out and

相关标签:
3条回答
  • 2021-02-01 10:33

    I ended up grouping my annotations in clusters using OCMapView. Its free, and very easy to implement into existing code.

    It automatically groups your annotations when zoomed out and as you zoom in they appear unclustered as they should.

    I know this is an old question but its a great option for anyone else trying to do the same thing.

    0 讨论(0)
  • 2021-02-01 10:39

    I would suggest a couple of things. One, look at the method annotationsInMapRect:. According to the docs, it says:

    This method offers a fast way to retrieve the annotation objects in a particular portion of the map. This method is much faster than doing a linear search of the objects in the annotations property yourself.

    Two, look at dequeueReusableAnnotationViewWithIdentifier:. According to the docs again, it says:

    For performance reasons, you should generally reuse MKAnnotationView objects in your map views. As annotation views move offscreen, the map view moves them to an internally managed reuse queue. As new annotations move onscreen, and your code is prompted to provide a corresponding annotation view, you should always attempt to dequeue an existing view before creating a new one. Dequeueing saves time and memory during performance critical operations such as scrolling.

    Finally, a couple of thoughts. Instead of doing these changes every time the - (void)mapView:(MKMapView *)_mapView regionDidChangeAnimated:(BOOL)animated method is called, how about doing it according to some other rule (such as after a timer gets fired [make sure to reset the timer every time this gets called])? Another thing to consider: how about grouping annotations together that are super close to each other? Let's say you're zoomed out to where Rhode Island looks super small, maybe just a dozen pixels wide, and you have 100 points in Rhode Island -- you should just display one pin.

    Hope this helps!

    0 讨论(0)
  • 2021-02-01 10:51

    As I understand your code, you are hiding all the annotations' views if the mapView is zoomed out more than a certain specified value.

    It seems to me that something more like the following would be better:

    - (void)mapView: (MKMapView*)_mapView regionDidChangeAnimated: (BOOL)animated
    {
        if (_mapView.region.span.latitudeDelta > .010 && self.mapViewsHidden == NO) {
            for (MapAnnotation* annotation in _mapView.annotations) {
                [[_mapView viewForAnnotation: annotation] setHidden: YES];
            }
            [self.warningLabel setHidden: NO];
            [self setMapViewsHidden: YES];
        }
        else if (_mapView.region.span.latitudeDelta <= .010 && self.mapViewsHidden == YES) {
            for (MapAnnotation* annotation in _mapView.annotations) {
                [[_mapView viewForAnnotation: annotation] setHidden: NO];
            }
            [self.warningLabel setHidden: YES];
            [self setMapViewsHidden: NO];
        }
    }
    

    With the above, in most cases the only thing this code does is a couple of if checks.

    Another solution would be to remove the annotations when they shouldn't show on the map. Personally, I think this would be better, that way the code doesn't have to create views for annotations that haven't been shown on the map yet.

    0 讨论(0)
提交回复
热议问题