我有一个定制对象的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不幸的是,这似乎没有得到匹配,即使只有一个注释在地图上。下面是从我的NSLog中记录的内容
37.627946 == 37.622267 && -122.431599 == -122.435596 && Testlocation == Testlocation奇怪的是,双值似乎有点过了,但我不知道为什么。
是否有更好的方法将注释与数组中的对象匹配,以便更新原始对象?
发布于 2012-10-17 18:37:50
坐标值很可能是"off“,因为注释已被拖到新位置。
即使值相等,我也不建议将浮点数作为对象相等性的测试。
相反,我建议以下选择:
Proximity类中添加对源ProximityAnnotation对象的引用,并在创建注释时设置它(例如。proximityAnnotation.sourceProximity = proximity;)。然后,要更新原始的Proximity对象,可以直接从注释本身获得对它的引用。ProximityAnnotation类,并使Proximity类本身实现MKAnnotation协议,在这种情况下,可能甚至不需要更新。https://stackoverflow.com/questions/12940768
复制相似问题