1

我的 MKMapView 上有大约 500 个注释,我用OCMapView将它们聚集在一起,替换了普通的 MKMapView。无论如何,我的注释是聚集的,但不是很好,这就是为什么我需要一点帮助。我看到了聚集的注释,它们相互更新得很好。如果我靠近它们,它们就会散开。到目前为止一切都很好,但是所有单个注释都被命名为 Cluster 并且它们的计数为零。也许这只是一个小问题/逻辑问题。对于一些理解这里一些代码给你:

#pragma mark - map delegate
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *annotationView;

    // if it's a cluster
    if ([annotation isKindOfClass:[OCAnnotation class]])
    {

        OCAnnotation *clusterAnnotation = (OCAnnotation *)annotation;

        annotationView = (MKAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:@"ClusterView"];
        if (!annotationView)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ClusterView"];
            annotationView.canShowCallout = YES;
            annotationView.centerOffset = CGPointMake(0, -20);
        }
        //calculate cluster region
        CLLocationDistance clusterRadius = mapView.region.span.longitudeDelta * mapView.clusterSize * 111000 / 2.0f; //static circle size of cluster

        MKCircle *circle = [MKCircle circleWithCenterCoordinate:clusterAnnotation.coordinate radius:clusterRadius * cos([annotation coordinate].latitude * M_PI / 180.0)];
        [circle setTitle:@"background"];
        [mapView addOverlay:circle];

        MKCircle *circleLine = [MKCircle circleWithCenterCoordinate:clusterAnnotation.coordinate radius:clusterRadius * cos([annotation coordinate].latitude * M_PI / 180.0)];
        [circleLine setTitle:@"line"];
        [mapView addOverlay:circleLine];
        NSLog(@"%@", annotationArray);

        // set title
        clusterAnnotation.title = @"Cluster";
        clusterAnnotation.subtitle = [NSString stringWithFormat:@"Containing annotations: %d", [clusterAnnotation.annotationsInCluster count]];

        // set its image
        annotationView.image = [UIImage imageNamed:@"Pin.png"];

        // change pin image for group
        if (mapView.clusterByGroupTag)
        {
            if ([clusterAnnotation.groupTag isEqualToString:kTYPE1])
            {
                annotationView.image = [UIImage imageNamed:@"bananas.png"]; //OC examples for debug
            }
            else if([clusterAnnotation.groupTag isEqualToString:kTYPE2])
            {
                annotationView.image = [UIImage imageNamed:@"oranges.png"]; //OC examples for debug
            }
            clusterAnnotation.title = clusterAnnotation.groupTag;
        }
    }
    // If it's a single annotation
    else if([annotation isKindOfClass:[OCMapViewHelpAnnotation class]])
    {
        OCMapViewHelpAnnotation *singleAnnotation = (OCMapViewHelpAnnotation *)annotation;
        annotationView = (MKAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:@"singleAnnotationView"];
        if (!annotationView)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:singleAnnotation reuseIdentifier:@"singleAnnotationView"];
            annotationView.canShowCallout = YES;
            annotationView.centerOffset = CGPointMake(0, -20);
        }
        singleAnnotation.title = singleAnnotation.groupTag;

        if ([singleAnnotation.groupTag isEqualToString:kTYPE1])
        {
            annotationView.image = [UIImage imageNamed:@"banana.png"];
        }
        else if([singleAnnotation.groupTag isEqualToString:kTYPE2])
        {
            annotationView.image = [UIImage imageNamed:@"orange.png"];
        }
    }
    // Error
    else
    {
        annotationView = (MKPinAnnotationView *)[aMapView dequeueReusableAnnotationViewWithIdentifier:@"errorAnnotationView"];
        if (!annotationView)
        {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"errorAnnotationView"];
            annotationView.canShowCallout = NO;
            ((MKPinAnnotationView *)annotationView).pinColor = MKPinAnnotationColorRed;
        }
    }

    return annotationView;
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKCircle *circle = overlay;
    MKCircleView *circleView = [[MKCircleView alloc] initWithCircle:overlay];

    if ([circle.title isEqualToString:@"background"])
    {
        circleView.fillColor = [UIColor yellowColor];
        circleView.alpha = 0.25;
    }
    else if ([circle.title isEqualToString:@"helper"])
    {
        circleView.fillColor = [UIColor redColor];
        circleView.alpha = 0.25;
    }
    else
    {
        circleView.strokeColor = [UIColor blackColor];
        circleView.lineWidth = 0.5;
    }

    return circleView;
}

- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated
{
    [mapView removeOverlays:mapView.overlays];
    [mapView doClustering];
}

我注意到它if([annotation isKindOfClass:[OCMapViewHelpAnnotation class]])从未被调用,但如果集群之外有一些注释,它必须调用。

感谢您的关注

编辑

注解

通常它充满了诸如“名称”和“街道”之类的信息,但在绘制后它会被“集群”和“包含注释:0”覆盖

编辑 2

- (void)loadKml:(NSURL *)url
{
    // parse the kml

    Parser *parser = [[Parser alloc] initWithContentsOfURL:url];
    parser.rowElementName = @"Placemark";
    parser.elementNames = @[@"name", @"Snippet", @"coordinates", @"description"];
    //parser.attributeNames = @[@"src"];
    [parser parse];

    // add annotations for each of the entries
    annotationArray = [[NSMutableArray alloc] init];

    for (NSDictionary *locationDetails in parser.items)
    {
        OCAnnotation *annotation = [[OCAnnotation alloc] init];
        annotation.title = locationDetails[@"name"];
        annotation.subtitle = locationDetails[@"Snippet"];
        NSArray *coordinates = [locationDetails[@"coordinates"] componentsSeparatedByString:@","];
        annotation.coordinate = CLLocationCoordinate2DMake([coordinates[1] floatValue], [coordinates[0] floatValue]);
        annotation.groupTag = annotation.title;
        [annotationArray addObject:annotation];
//        NSLog(@"%@", annotation.title);
    }
    [self.mapView addAnnotations:annotationArray];
}
4

1 回答 1

2

我是 OCMapView 的开发者。您能否解释一下,如何重现您的问题?
这听起来很不寻常,因为您已经发布了示例代码......


更新:
该错误隐藏在loadKml:您创建OCAnnotation.
OCAnnotation由 byOCMapView为集群保留。您不应该使用OCAnnotation或任何子类作为自己的注释。

于 2013-04-10T11:22:18.007 回答