gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:01:06 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com