首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将MKMapView上的注释与创建它们的源数组中的注释匹配

将MKMapView上的注释与创建它们的源数组中的注释匹配
EN

Stack Overflow用户
提问于 2012-10-17 18:19:40
回答 1查看 189关注 0票数 1

我有一个定制对象的NSArray,称为Proximity。我通过创建一个新的ProximityAnnotation (如下所示)将它们添加到地图中:

代码语言:javascript
复制
// 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对象。我正试图通过以下步骤来做到这一点:

代码语言:javascript
复制
- (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中记录的内容

代码语言:javascript
复制
37.627946 == 37.622267 && -122.431599 == -122.435596 && Testlocation == Testlocation

奇怪的是,双值似乎有点过了,但我不知道为什么。

是否有更好的方法将注释与数组中的对象匹配,以便更新原始对象?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-17 18:37:50

坐标值很可能是"off“,因为注释已被拖到新位置。

即使值相等,我也不建议将浮点数作为对象相等性的测试。

相反,我建议以下选择:

  • Proximity类中添加对源ProximityAnnotation对象的引用,并在创建注释时设置它(例如。proximityAnnotation.sourceProximity = proximity;)。然后,要更新原始的Proximity对象,可以直接从注释本身获得对它的引用。
  • 消除ProximityAnnotation类,并使Proximity类本身实现MKAnnotation协议,在这种情况下,可能甚至不需要更新。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12940768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档