在同一坐标上分组注释引脚

时间:2014-12-18 16:16:54

标签: ios objective-c mapkit

我的地图上有很多注释,如何在同一坐标上对注释引脚进行分组?我发现this但我不知道如何使用它

2 个答案:

答案 0 :(得分:0)

我认为你的意思是在地图上使用类似群集的东西来分组别针,不是吗? 我曾经使用过REVClusterMap: https://github.com/RVLVR/REVClusterMap

避免在地图上显示许多点非常有用。这样,当您远离地图时,您将显示注释组(聚类),当您放大时,这些点将会出现。

您只需要像这样定义地图:

REVClusterMapView *mapView;

mapView = [[REVClusterMapView alloc] initWithFrame:viewBounds];

当你必须添加和定义注释时,它将是这样的:

NSMutableArray *pins = [NSMutableArray array];

    for(int i=0;i<50;i++) {
        ...

        CLLocationCoordinate2D newCoord = {lat+latDelta, lng+lonDelta};

        REVClusterPin *pin = [[REVClusterPin alloc] init];

        pin.title = [NSString stringWithFormat:@"Pin %i",i+1];;
        pin.subtitle = [NSString stringWithFormat:@"Pin %i subtitle",i+1];
        pin.coordinate = newCoord;
        [pins addObject:pin];
        [pin release];
    }

    [mapView addAnnotations:pins];


-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
...
REVClusterPin *pin = (REVClusterPin *)annotation;

    MKAnnotationView *annView;

    if( [pin nodeCount] > 0 ) { //cluster

        pin.title = @"___";

        annView = (REVClusterAnnotationView*)
        [mapView dequeueReusableAnnotationViewWithIdentifier:@"cluster"];

        if( !annView )
            annView = (REVClusterAnnotationView*)
            [[REVClusterAnnotationView alloc] initWithAnnotation:annotation
                                                  reuseIdentifier:@"cluster"];

        annView.image = [UIImage imageNamed:@"cluster.png"];

        [(REVClusterAnnotationView*)annView setClusterText:
         [NSString stringWithFormat:@"%i",[pin nodeCount]]];

        annView.canShowCallout = NO;
    } 
    else { //show your code for a single annotation

...
}

我希望它会有用:)

答案 1 :(得分:0)

拥有ADClusterMapView?它对我们来说非常好。