通过拖动 Pin 图在 MKMapView 上进行反向地理编码
我有 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.
我想通了。
由于 KVO,我必须设置 Location 对象的副标题。这样就可以显示出来了。
但是因为 subtilte 是由我生成的,所以
我只需在我的 NSManagedObject 子类中执行以下操作。
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
I just had to do the following in my NSManagedObject Subclass.