MKMapView には約 500 の注釈があり、通常の MKMapView を置き換えるOCMapViewでそれらをクラスター化しました。とにかく、私の注釈はクラスター化されていますが、あまりよくないので、少し助けが必要です。クラスター化された注釈が表示され、互いにかなりうまく更新されます。私が彼らに近づくと、彼らは群がっていません。ここまでは順調ですが、単一の注釈はすべて 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];
}