如何根据缩放级别自动分组MKAnnotations? [英] How can I group MKAnnotations automatically regarding zoom level?

查看:126
本文介绍了如何根据缩放级别自动分组MKAnnotations?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户缩小MKMapView,我希望彼此接近的MKAnnotations自动分组为一个组注释。
如果用户缩小,则组注释应再次拆分为唯一/原始注释。

if the user zooms out on a MKMapView, i want MKAnnotations which are near to each other automatically grouped into one "group" annotation. if the user zooms back in, the "group" annotation should be split again to the unique/original annotations.

apple已在iOS 4中执行此操作Photos.app

apple does this already in the iOS 4 Photos.app

是否有一种常见的预定义方式来执行此操作?

is there a common, "predefined" way to do this?

推荐答案

它正常使用地图上超过1500个注释:

Its normal working with more than 1500 annotations on the map:

-(void)mapView:(MKMapView *)mapView_ regionDidChangeAnimated:(BOOL)animated
{
    NSMutableSet * coordSet = [[NSMutableSet alloc] init];

    for(id<MKAnnotation> an in mapView_.annotations)
    {
        if([an isKindOfClass:[MKUserLocation class]])
            continue;

        CGPoint point = [mapView_ convertCoordinate:an.coordinate toPointToView:nil];
        CGPoint roundedPoint;

        roundedPoint.x = roundf(point.x/10)*10;
        roundedPoint.y = roundf(point.y/10)*10;

        NSValue * value = [NSValue valueWithCGPoint:roundedPoint];

        MKAnnotationView * av = [mapView_ viewForAnnotation:an];

        if([coordSet containsObject:value])
        {
            av.hidden = YES;
        }
        else
        {
            [coordSet addObject:value];
            av.hidden = NO;
        }
    }

    [coordSet release];
}

这篇关于如何根据缩放级别自动分组MKAnnotations?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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