Draggable annotation with iOS : callout info update
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 meth开发者_运维技巧od 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 !
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];
}
Another option is to set annotationView.canShowCallout = NO
just before starting the reverse geocoder, and then set the flag back to YES when done.
精彩评论