Which annotation's subtitle is set by MKReverseGeocoder
I am using MKReverseGeocoder to reversely geocode a latitude/longitude address. However I am having trouble determining which Annotation's subtitle is going to be set. To expand on that a bit, I am calling a reverse geocode on each annotation to determine the address, and then I would like to set their subtitle text to their address. I have managed to do it for the first annotation, but I am unsure on how I would do it for the second annotation.
Here is my code:
- (void)loadAnnotations {
Annotation *annotation1 = [[Annotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(a,b)];
annotation1.title = @"Anno1n";
CLLocation *annoe = [[CLLocation alloc] initWithLatitude:annotation1.coordinate.latitude longitude:annotation1.coordinate.longitude];
CLLocation *usrloc = [[CLLocation alloc] initWithLatitude:mapView.userLocation.coordinate.latitude longitude:mapView.userLocation.coordinate.longitude];
NSLog(@"%.2f km", [annoe distanceFromLocation:usrloc]/1000);
MKReverseGeocoder *geo = [[MKReverseGeocoder alloc] initWithCoordinate:annotation1.coordinate];
[geo setDelegate:self];
[geo start];
[mapView addAnnotation:annotation1];
Annotation *annotation2 = [[Annotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(c,d)];
annotation2.title = @"Anno2";
geo = [[MKReverseGeocoder alloc] initWithCoordinate:annotation2.coordinate];
[geo start];
[mapView addAnnotation:annotation2];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
NSArray *array = [[placemark addressDictionary] objectForKey:@"FormattedAddressLines"];
if (geocoder.coordinate.latitude == mapView.userLocation.coordinate.latitude && geocoder.coordinate.longitude) {
mapView.userLocation.title = @"Current Location";
mapView.userLocation.subtitle = [NSString stringWithFormat:@"%@ %@", [array objectAtIndex:0], [array objectAtIndex:1]];
}
else {
Annotation *annot = (Annotation *)[[mapView annotations] objectAtIndex:1];
开发者_开发百科 [annot setSubtitle:[NSString stringWithFormat:@"%@ %@", [array objectAtIndex:0], [array objectAtIndex:1]]];
//NSLog(@"%@", [placemark addressDictionary]);
}
}
please let me know how I can do this.
Looping through your annotations and reverse geocoding each one is not recommended. There's a usage limit for the reverse geocoding service. Apple recommends developers should only call the reverse geocoding service when triggered by user action, like selecting the annotation.
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/Reference/Reference.html%23//apple_ref/occ/cl/MKReverseGeocoder
精彩评论