我的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];
}
答案 0 :(得分:2)
我是OCMapView的开发人员。你能解释一下,如何重现你的问题? 这听起来很不寻常,因为你已经发布了示例代码......
<强>更新强>
该错误会隐藏在您创建loadKml:
的{{1}}方法中
OCAnnotation
由OCAnnotation
为群集保留OCMapView
。您不应将OCAnnotation
或任何子类用作自己的注释。