Reputation: 5187
I'm trying to forward geocode 2 place names into coordinates with following code:
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:place inRegion:nil completionHandler:^(NSArray* placemarks, NSError* error){ NSLog(@"a"); NSLog(@"count %d", [placemarks count]); for (CLPlacemark* aPlacemark in placemarks) { CLLocationCoordinate2D coord = aPlacemark.location.coordinate; NSLog(@"%f, %f", coord.latitude, coord.longitude); } }]; [geocoder geocodeAddressString:place inRegion:nil completionHandler:^(NSArray* placemarks, NSError* error){ NSLog(@"b"); NSLog(@"count %d", [placemarks count]); for (CLPlacemark* aPlacemark in placemarks) { CLLocationCoordinate2D coord = aPlacemark.location.coordinate; NSLog(@"%f, %f", coord.latitude, coord.longitude); } }];
To simplify, I convert a single place name twice. When I run the code, only the first geocoding completion handler is runed. Remaining geocoding completion handlers are ignored.
I'd like to know why does it happen and how to convert more than one places.
Upvotes: 1
Views: 882
Reputation: 4977
See Apple's guidance: http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLGeocoder_class/Reference/Reference.html
Applications should be conscious of how they use geocoding. Here are some rules of thumb for using this class effectively:
1) Send at most one geocoding request for any one user action.
2) If the user performs multiple actions that involve geocoding the same location, reuse the results from the initial geocoding request instead of starting individual requests for each action.
3) When you want to update the user’s current location automatically (such as when the user is moving), issue new geocoding requests only when the user has moved a significant distance and after a reasonable amount of time has passed. For example, in a typical situation, you should not send more than one geocoding request per minute.
4) Do not start a geocoding request at a time when the user will not see the results immediately. For example, do not start a request if your application is inactive or in the background.
Upvotes: 1
Reputation: 113757
You're not supposed to do more than one geocoding operation at a time. The block happens asynchronously, so the second geocoding operation will probably start before the first one has a chance to finish. Here's the docs:
This method submits the specified location data to the geocoding server asynchronously and returns. Your completion handler block will be executed on the main thread. After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request.
Upvotes: 1