Domanda

i have to execute a high number of reverseGeocodeLocation request, i use this method for doing that:

 for (Photo *photo in arrayWhitPicture) {
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [queue addOperationWithBlock:^{
            [geocoder reverseGeocodeLocation:[[CLLocation alloc] initWithLatitude:[photo.latitude doubleValue] longitude:[photo.longitude doubleValue]] completionHandler:^(NSArray *placemarks, NSError *error) {
                if (error){
                    NSLog(@"Geocode failed with error: %@", error);
                    return;
                }
                CLPlacemark *myPlacemark = [placemarks objectAtIndex:0];
                NSString *city = myPlacemark.locality;
                NSLog(@"My country code: %@", city);

            }];
        }];
    }

this code actual work, but the problem is that some of these request (like half) get this error:

Error Domain=kCLErrorDomain Code=2

after a few research i think this happen because i do a lot of request in short amount of time in fact the apple documentations say:

Send at most one geocoding request for any one user action.

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.

so my question is: my error is really caused by the fact that i do a lot of request, and in that case what can i do to resolve this problem, do you know other system apart to use the reverseGeocoding?

È stato utile?

Soluzione

I have had exactly this problem, and my solution was to throttle down the number of reverse geocode queries that I performed (i.e. temporarily suspend the queue if already processed a whole bunch). This worked so well that I did not have to implement plan B, which was to switch to a different service.

See for example this question for discussion of alternate services. Google has a similar limit of 2500 requests per API key and 24 hour period. There is also Bing.

Altri suggerimenti

Apple most definitely limits how many geocoding requests you can issue at a time. Other users are reporting that the limit is around 50, though that could change at any time. The recommendation seems to be to do the geocoding in batches and to issue only one batch at a time, starting each batch only after the previous one completes.

Apple limits to perform number of Reverse GeoCode requests your application can make at a time. Sometimes I have seen this limited to ONE.

The solution is to implement your own Reverse Geocoder Queue (you can implement it as a separate class), in which you can add all of your requests. This queue need to execute one request at a time and after first is done, execute next. You can add callback blocks to notify you once the reverse geocoding is done for each request.

Example API in the Reverse Geocoder queue class can be like:

- (void) reverseGeocodeLocation: (CLLocation *) location completion: (CLGeocodeCompletionHandler) completionHandler
{
    // Create some queue (NSMutableArray) in the class
    // Create some ReverseGeoLocationObject with location and completionHandler as members
   // Add ReverseGeoLocationObject to queue
   // Check is queue is not already processing. If NO then process next request. You have have API named processNextRequest which you can call here (put code you posted in this API for single request)
}

Also call processNextRequest when CLGeocoder returns.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top