我在我的MKMapView上有大约500个注释,我用OCMapView将它们聚集在一起,这个集合取代了普通的MKMapView。无论如何,我的注释聚集在一起,但不是很好,为什么我需要一点帮助。我看到聚集的注释,他们互相更新很好。如果我放大他们附近他们uncluster。到目前为止这么好,但所有单个注释都被命名为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];
}
我刚刚添加了一张图片以更好地显示它。我试着保持代码简单,只实现我自己的编码周围的注释。 – CTSchmidt 2013-04-10 11:35:34
好吧,我明白问题是什么,但不知道如何重现它。看到更多的代码,特别是如何添加注释会很有趣。 – yinkou 2013-04-10 11:48:15
我使用一个kml文件来存储我的位置和一堆信息。 – CTSchmidt 2013-04-10 11:54:49