我试图在将注释添加到数组之前检查它是否存在。我不明白为什么 [visibleAnnotations containsObject:annotation] 总是返回 False。
MKMapRect mRect = self.locationView.visibleMapRect;
NSSet *visibleAnnotations =[self.locationView annotationsInMapRect:mRect];
NSLog(@"Annotation in Rect %@",visibleAnnotations);
for(NSArray * obj in JSON){
coordinates.latitude=[[obj valueForKey:@"Latitude"] doubleValue];
coordinates.longitude=[[obj valueForKey:@"Longitude"] doubleValue];
NSString *title=[NSString stringWithFormat:@"%@",[obj valueForKey:@"DeviceId"]];
MapAnnotation *annotation = [[MapAnnotation alloc] initWithTitle:title andCoordinate:coordinates] ;
NSLog(@"New Annotation %@",annotation);
if ([visibleAnnotations containsObject:annotation ]) {
//[addPins addObject:annotation];
NSLog(@"Contains obj");
}
[addPins addObject:annotation];
[annotation release];
}
缩放至仅显示 1 个注释。
Annotation in Rect {(<MapAnnotation: 0x1cd80720>)}
New Annotation <MapAnnotation: 0x1cd79410>
谢谢