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
    协议,在这种情况下甚至可能不需要更新

我采纳了你的第一个建议,效果非常好!真的不知道为什么我一开始没有想到这一点!:)我采纳了你的第一个建议,效果非常好!真的不知道为什么我一开始没有想到这一点!:)