iOS 可拖动注释:标注信息更新

发布于 2024-10-12 11:45:15 字数 1487 浏览 2 评论 0原文

我在可拖动注释的标注信息方面遇到问题:我使用 MKReverseGeocoder 来获取与注释位置相对应的坐标地址。

但有一些我无法控制的事情:每次删除可拖动注释时,都会显示标注信息。很多时候,MKReverseGeocoder 没有时间在这种情况发生之前更新地址信息。因此,在通常情况下,注释标注会显示与注释的先前坐标相对应的地址信息。

这是代码:

1)删除注释时调用的委托方法:

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState{
        if (oldState == MKAnnotationViewDragStateDragging) {
            MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:annotationView.annotation.coordinate];
            reverseGeocoder.delegate = self;
            [reverseGeocoder start];
            while (reverseGeocoder.querying) {
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
            }
            [reverseGeocoder release];
        }   
    }

2)reverseGeocoder 找到答案时调用的委托方法:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
    NSString *newSubtitle = [NSString stringWithFormat:@"%@ %@, %@ %@, %@",placemark.subThoroughfare,placemark.thoroughfare,placemark.postalCode,placemark.locality, placemark.country];
    Grocery *draggableGrocery = [myMapView.annotations objectAtIndex:0];
    draggableGrocery.address = newSubtitle;
    self.addressNewGrocery = newSubtitle;
}

有什么想法吗?

谢谢 !

I'm having a problem with the callout info of a draggable annotation : I use an MKReverseGeocoder to get the address of the coordinates corresponding to the annotation position.

But there is something I don't manage to control : every time the draggable annotation is dropped, the callout info shows up. Very often, the MKReverseGeocoder did not have time to update the address info before this happens. So in those usual cases, the annotation callout shows the address info corresponding to the previous coordinates of the annotation.

Here is the code :

1)Delegate method called when annotation is dropped :

 - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState{
        if (oldState == MKAnnotationViewDragStateDragging) {
            MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:annotationView.annotation.coordinate];
            reverseGeocoder.delegate = self;
            [reverseGeocoder start];
            while (reverseGeocoder.querying) {
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
            }
            [reverseGeocoder release];
        }   
    }

2)Delegate method called when reverseGeocoder finds an answer :

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
    NSString *newSubtitle = [NSString stringWithFormat:@"%@ %@, %@ %@, %@",placemark.subThoroughfare,placemark.thoroughfare,placemark.postalCode,placemark.locality, placemark.country];
    Grocery *draggableGrocery = [myMapView.annotations objectAtIndex:0];
    draggableGrocery.address = newSubtitle;
    self.addressNewGrocery = newSubtitle;
}

Any idea ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小红帽 2024-10-19 11:45:15

我本来想发布同样的问题,从昨天开始我一直在寻找同样的问题。


更新:


我刚刚找到了一种方法,我不确定它是否正确。知道我有一个名为 selectedAnnotation 的 MKAnnotation 子类,我只是取消选择,然后在 revereseGeocoder:didFindPlacemark 函数中获得新标题后选择该对象:

[mapView deselectAnnotation:selectedAnnotation animated:NO];
[mapView selectAnnotation:selectedAnnotation animated:NO];

对于您的情况,您的代码应如下所示:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
    NSString *newSubtitle = [NSString stringWithFormat:@"%@ %@, %@ %@, %@",placemark.subThoroughfare,placemark.thoroughfare,placemark.postalCode,placemark.locality, placemark.country];
    Grocery *draggableGrocery = [myMapView.annotations objectAtIndex:0];
    draggableGrocery.address = newSubtitle;
    self.addressNewGrocery = newSubtitle;
    //  add the following lines [I assumed that draggableGrocery [Grocery class] is a subclass of MKAnnotation]
    [mapView deselectAnnotation:draggableGrocery animated:NO];
    [mapView selectAnnotation:draggableGrocery animated:NO];
}

I was to post the same question, I've been searching for the same issue since yesterday.


UPDATE:


I just figured out a way, and I'm not sure if it is the right one. Knowing that I have a MKAnnotation subclass called selectedAnnotation, I simply deselected, then selected that object after I got the new title in the revereseGeocoder:didFindPlacemark function:

[mapView deselectAnnotation:selectedAnnotation animated:NO];
[mapView selectAnnotation:selectedAnnotation animated:NO];

for your case your code should look like this :

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
    NSString *newSubtitle = [NSString stringWithFormat:@"%@ %@, %@ %@, %@",placemark.subThoroughfare,placemark.thoroughfare,placemark.postalCode,placemark.locality, placemark.country];
    Grocery *draggableGrocery = [myMapView.annotations objectAtIndex:0];
    draggableGrocery.address = newSubtitle;
    self.addressNewGrocery = newSubtitle;
    //  add the following lines [I assumed that draggableGrocery [Grocery class] is a subclass of MKAnnotation]
    [mapView deselectAnnotation:draggableGrocery animated:NO];
    [mapView selectAnnotation:draggableGrocery animated:NO];
}
樱娆 2024-10-19 11:45:15

另一种选择是在启动反向地理编码器之前设置 annotationView.canShowCallout = NO,然后在完成后将标志设置回 YES。

Another option is to set annotationView.canShowCallout = NO just before starting the reverse geocoder, and then set the flag back to YES when done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文