I am using Google to take an address to get its lat and long. I run through about 60-65 address. Each time though I will randomly get 5 or 6 that return a 0,0 coordinate pair.
Every time the indexes that fail are different so I am sure that it is not an address problem.
Here is the code that I am using to return the lat and long
Would it possibly be that I am calling this method from inside a for loop? Maybe calling it over and over so fast will cause it to hick-up sometimes?
-(CLLocationCoordinate2D)addressLocation:(NSString*)_address city:(NSString*)_city state:(NSString*)_state {
NSString *temp = [NSString stringWithFormat:@"%@, %@, %@", _address, _city, _state];
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [temp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError* error = nil;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
NSLog(@"Error: %@", [error description]);;
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
EDIT
I have found a work around for the moment if anyone else if having trouble with this.
I go through my list once finding the lat and long of each address.
If there is an error returned from the Google query I add that address to a holding array. Meaning that I passed the rate limit mentioned by Firoze.
After the initial run through is done, I go through the holding array and find the locations of each address.
I am sure that there is a much more cleaner implementation of this but for anyone who is not able to obtain a business license this is a hacked way around.
I hope this helps,
Cory
You should check to see if locationString is nil. And if so, look at the error. My guess is that your calls are being rate limited.
You should probably look at Google’s terms of service for this service.