Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

iphone - How can I group MKAnnotations automatically regarding zoom level?

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 does this already in the iOS 4 Photos.app

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...