处理大量的MKMapView注释 [英] Handling large quantity of MKMapView Annotations

查看:94
本文介绍了处理大量的MKMapView注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有大量注释(3000+)的地图视图,当用户缩放到合理的水平时,一切都很好。

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 a large quanitiy of annotations come into view there is a lot of slow down due to the sheer amount of annotations being shown at once. What is the best way to handle this?

我正在使用的当前解决方案:

The current solution i am using:

- (void)mapView:(MKMapView *)_mapView regionDidChangeAnimated:(BOOL)animated {

    NSArray *annotations = [_mapView annotations];  
    MapAnnotation *annotation = nil; 

    for (int i=0; i<[annotations count]; i++)
    {
        annotation = (MapAnnotation*)[annotations objectAtIndex:i];
        if (_mapView.region.span.latitudeDelta > .010)
        {
            [[_mapView viewForAnnotation:annotation] setHidden:YES];
            warningLabel.hidden = NO;
        }
        else {
            [[_mapView viewForAnnotation:annotation] setHidden:NO];
            warningLabel.hidden = YES;
        }
    }
}

尽管由于在放大和缩小以及滚动时,这会导致大量减速。我似乎无法想出一个更好的方法来处理这个问题,有没有办法只循环显示当前正在显示的注释或沿着那些线来减少循环的大小?

Works great although due to the sheer size of the loop this causes a lot of slow down when zooming in and out and scrolling around. I can't seem to think of a better way to handle this, is there a way to only loop through the annotations currently being displayed or something along those lines to reduce the size of the loop?

推荐答案

据我了解你的代码,如果mapView的缩小超过某个指定值,你就会隐藏所有注释的视图。

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];
    }
}

有了上述内容,在大多数情况下这是唯一的事情代码确实是几个if检查。

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.

这篇关于处理大量的MKMapView注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆