通过拖动 Pin 图在 MKMapView 上进行反向地理编码

发布于 2024-10-06 00:57:34 字数 1778 浏览 2 评论 0原文

我有 NSManagedObject 的子类“Location”,该类符合 MKAnnotation。

当我向地图添加位置时...ReverseGeocoder 启动。通过单击标注中看到的引脚,可以查看引脚的地址。所以一切都很好。

现在我的问题。

我能够拖动图钉。当我完成拖动后,ReverseGeocoder 再次开始请求位置数据。但尽管位置对象中的地标已更新,但标注视图中仍然有旧地址。再次拖动后我可以看到新的地址。 (当然新的已经不新了,因为我拖了)。

这是我的拖动状态代码:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState{

//if dragging ended
if (newState == MKAnnotationViewDragStateNone && oldState == MKAnnotationViewDragStateEnding) {

    CLLocation *location = [[CLLocation alloc] initWithLatitude:annotationView.annotation.coordinate.latitude longitude:annotationView.annotation.coordinate.longitude];
    [self geocodeLocation:location forAnnotation:annotationView.annotation];
    [location release];
}

以及我的反向地理编码器委托的代码

- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFindPlacemark:(MKPlacemark*)place
{
    Location*    theAnnotation = [self.map annotationForCoordinate:place.coordinate];
    if (!theAnnotation)
        return;
    // Associate the placemark with the annotation.
    theAnnotation.city = [place locality];
    theAnnotation.zipcode = [place postalCode];
    theAnnotation.street = [place thoroughfare];

    DEBUGLOG(@"Place: %@", place);


    // Add a More Info button to the annotation's view.
    MKPinAnnotationView*  view = (MKPinAnnotationView*)[self.map viewForAnnotation:theAnnotation];
    if (view)
    {
        DEBUGLOG(@"SUBTITLE: %@", theAnnotation.subtitle);
        view.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    }
}

任何帮助都会很棒。最后,地图应用程序是通过拖动来实现这一点的最佳示例。

I have a subclass "Location" of NSManagedObject and this class conforms to MKAnnotation.

When I add a Location to a Map...the ReverseGeocoder starts. By clicking the pin I see in the callout View the address of the pin. So everything works fine.

Now my problem.

I am able to drag the pins. When I am finished dragging, than again the ReverseGeocoder starts to ask for location Data. But I still have the old address in the callout view, although the placemarks are updated in the Location object. After dragging again I can see the new address. (of course the new one isn't new anymore, because I dragged).

Here is my code for the dragging state:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState{

//if dragging ended
if (newState == MKAnnotationViewDragStateNone && oldState == MKAnnotationViewDragStateEnding) {

    CLLocation *location = [[CLLocation alloc] initWithLatitude:annotationView.annotation.coordinate.latitude longitude:annotationView.annotation.coordinate.longitude];
    [self geocodeLocation:location forAnnotation:annotationView.annotation];
    [location release];
}

And the Code for my reverse geocoder delegate

- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFindPlacemark:(MKPlacemark*)place
{
    Location*    theAnnotation = [self.map annotationForCoordinate:place.coordinate];
    if (!theAnnotation)
        return;
    // Associate the placemark with the annotation.
    theAnnotation.city = [place locality];
    theAnnotation.zipcode = [place postalCode];
    theAnnotation.street = [place thoroughfare];

    DEBUGLOG(@"Place: %@", place);


    // Add a More Info button to the annotation's view.
    MKPinAnnotationView*  view = (MKPinAnnotationView*)[self.map viewForAnnotation:theAnnotation];
    if (view)
    {
        DEBUGLOG(@"SUBTITLE: %@", theAnnotation.subtitle);
        view.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    }
}

Any help would be wonderful. In the end the Maps App is best example for doing that by dragging.

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

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

发布评论

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

评论(1

怕倦 2024-10-13 00:57:34

我想通了。

由于 KVO,我必须设置 Location 对象的副标题。这样就可以显示出来了。

但是因为 subtilte 是由我生成的,所以

// Associate the placemark with the annotation.
theAnnotation.city = [place locality];
theAnnotation.zipcode = [place postalCode];
theAnnotation.street = [place thoroughfare];

我只需在我的 NSManagedObject 子类中执行以下操作。

- (void) setSubtitle:(NSString *)title{ //do nothing}

I figured out.

Because of KVO I hust hace to set the subtitle of the Location object. So it gets displayed.

But because the subtilte is generated by

// Associate the placemark with the annotation.
theAnnotation.city = [place locality];
theAnnotation.zipcode = [place postalCode];
theAnnotation.street = [place thoroughfare];

I just had to do the following in my NSManagedObject Subclass.

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