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