我有一个NSArray
称为Proximity
. ProximityAnnotation
我通过创建一个新的如下来将这些添加到地图中:
// Add the annotations to the map
if (self.proximityItems) {
for (Proximity *proximity in self.proximityItems) {
// Create a pin
ProximityAnnotation *proximityAnnotation = [[ProximityAnnotation alloc] init];
proximityAnnotation.coordinate = CLLocationCoordinate2DMake([proximity.latitude doubleValue], [proximity.longitude doubleValue]);
proximityAnnotation.title = proximity.title;
proximityAnnotation.subtitle = NSLocalizedString(@"Drag to change location", nil);
[self.map addAnnotation:proximityAnnotation];
}//end
// Create the map rect
MapUtility *util = [[MapUtility alloc] init];
[util zoomMapViewToFitAnnotations:self.map animated:YES];
}//end
这很好用。
现在,当我拖动注释时,我想更新数组ProximityAnnotation
中包含的相应对象proximityItems
。我正在尝试通过执行以下操作来做到这一点:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
// Get the coordiante
if ([annotationView.annotation isKindOfClass:[ProximityAnnotation class]] && newState == MKAnnotationViewDragStateEnding) {
ProximityAnnotation *annotation = (ProximityAnnotation *)annotationView.annotation;
CLLocationCoordinate2D coordinate = annotation.coordinate;
// Find the annotation that matches
for (Proximity *proximity in self.proximityItems) {
NSLog(@"%f == %f && %f == %f && %@ == %@", [proximity.latitude doubleValue], coordinate.latitude, [proximity.longitude doubleValue], coordinate.longitude, annotation.title, proximity.title);
if ([proximity.latitude doubleValue] == coordinate.latitude && [proximity.longitude doubleValue] == coordinate.longitude && [annotation.title isEqualToString:proximity.title]) {
// Update the proximity item
proximity.longitude = [NSNumber numberWithDouble:coordinate.longitude];
proximity.latitude = [NSNumber numberWithDouble:coordinate.latitude];
break;
}
}//end
}//end
}//end
不幸的是,即使地图上只有 1 个注释,这似乎也没有匹配。以下是我的记录NSLog
:
37.627946 == 37.622267 && -122.431599 == -122.435596 && Testlocation == Testlocation
奇怪的是,双值似乎有点偏离,但我不知道为什么。
有没有更好的方法将注释与我的数组中的对象相匹配,以便我可以更新该原始对象?