ios - 聚类注解覆盖非聚类注解

标签 ios annotations mkmapview mapkit

我的 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]]) 从未被调用,但如果集群外部有一些注释,则必须调用。

感谢您的关注

编辑

Annotation

通常它充满了像“姓名”和“街道”这样的信息,但是在绘制之后它被“集群”和“包含注释: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];
}

最佳答案

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


更新:
该错误隐藏在您创建 OCAnnotationloadKml: 方法中。
OCAnnotationOCMapView 保留用于集群。您不应使用 OCAnnotation 或任何子类作为自己的注释。

关于ios - 聚类注解覆盖非聚类注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15903525/

相关文章:

java - 抽象枚举一个注解属性类型

java - 如何为 JUnit 测试声明 "mixins"?

iphone - 拖动图钉时自动滚动 mkmapview

ios - 如何将自定义单元格与右细节和基本单元格对齐?

ios - 在 swift 中,如何在原型(prototype)单元格中将行(上面有按钮)推送到不同的 View Controller ?

iOS swift : can't Retrieve current url loaded in UIwebView

ios - pushViewController 似乎不适用于 Storyboard和 QLPreviewController

Java - 使用注解自动实现服务定位器模式

ios - 如何使用蓝点在我的 MKMapView 上显示我的当前位置和区域?

ios - 不透明的 UIView 不允许我在它后面滚动 UIView