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

时间:2011-01-15 18:05:36

标签: ios annotations mapkit draggable

我对可拖动注释的标注信息有疑问:我使用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;
}

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

我发布了同样的问题,自昨天以来我一直在寻找同样的问题。


更新:


我只想出办法,我不确定它是不是正确的。知道我有一个名为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];
}

答案 1 :(得分:0)

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