Ios 将MKMapView上的批注与创建它们的源数组中的批注相匹配
我有一个自定义对象的Ios 将MKMapView上的批注与创建它们的源数组中的批注相匹配,ios,mkmapview,nspredicate,mkannotation,mkannotationview,Ios,Mkmapview,Nspredicate,Mkannotation,Mkannotationview,我有一个自定义对象的NSArray,称为approxity。我通过创建一个新的ProximityAnnotation将它们添加到地图中,如下所示: // Add the annotations to the map if (self.proximityItems) { for (Proximity *proximity in self.proximityItems) { // Create a pin ProximityAnnotation *proxi
NSArray
,称为approxity
。我通过创建一个新的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
- (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
这很有效
现在,当我拖动注释时,我想更新包含在我的proximityItems
数组中的相应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
- (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
奇怪的是,这两个值似乎有点偏离,但我不知道为什么
是否有更好的方法将注释与数组中的对象匹配,以便更新原始对象?坐标值很可能为“关闭”,因为注释已被拖动到新位置 即使值相等,我也不建议将比较浮点数作为对象相等性的测试 相反,我建议以下选项:
- 在
类中添加对源ProximityAnnotation
对象的引用,并在创建注释时设置它(例如ProximityAnnotation
)。然后,要更新原始的ProximityAnnotation.sourceproximition=proximition;
对象,可以直接从注释本身获取对它的引用approxity
- 消除
类,并使ProximityAnnotation
类本身实现ProximityAnnotation
协议,在这种情况下甚至可能不需要更新MKAnnotation
- 在
类中添加对源ProximityAnnotation
对象的引用,并在创建注释时设置它(例如ProximityAnnotation
)。然后,要更新原始的ProximityAnnotation.sourceproximition=proximition;
对象,可以直接从注释本身获取对它的引用approxity
- 消除
类,并使ProximityAnnotation
类本身实现ProximityAnnotation
协议,在这种情况下甚至可能不需要更新MKAnnotation