Адрес для согласования в цели c на карте Google
У меня есть 60 адресов, и я хочу добавить пин-код для каждого адреса. Я хочу использовать карты Google, а не карты Apple. У меня есть цикл снаружи, и вызывает этот метод для 100 элементов.
И иногда я получал эту ошибку: *** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array
И ошибка не является регулярной, я имею в виду, что ошибка не соответствует точному адресу. Иногда я получаю 20 ошибок, иногда 15,...
Кто-нибудь знает как это решить? благодарю вас
- (CLLocationCoordinate2D) getLocationFromAddress:(NSString*) address
{
NSError *error = nil;
NSString *lookUpString;
NSDictionary *jsonDict;
NSData *jsonResponse;
NSArray *locationArray;
lookUpString = [[NSString alloc] initWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", address];
lookUpString = [lookUpString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
jsonResponse = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:lookUpString]];
jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];
locationArray = [[NSArray alloc] init];
locationArray = [[[jsonDict valueForKey:@"results"] valueForKey:@"geometry"] valueForKey:@"location"];
@try {
locationArray = [locationArray objectAtIndex:0];
NSLog(@" LOADING, %d, %d, %d", [locationArray count], [jsonDict count], [jsonResponse length]);
}
@catch (NSException *exception) {
NSLog(@"ERROR LOADING, %d, %d, %d", [locationArray count], [jsonDict count], [jsonResponse length]);
}
@finally {
}
NSString *latitudeString = [[NSString alloc] init];
latitudeString = [locationArray valueForKey:@"lat"];
NSString *longitudeString = [[NSString alloc] init];
longitudeString = [locationArray valueForKey:@"lng"];
NSString *statusString = [[NSString alloc] init];
statusString = [jsonDict valueForKey:@"status"];
double latitude = 0.0;
double longitude = 0.0;
if ([statusString isEqualToString:@"OK"])
{
latitude = [latitudeString doubleValue];
longitude = [longitudeString doubleValue];
}
else
NSLog(@"Something went wrong, couldn't find address");
_location.longitude = longitude;
_location.latitude = latitude;
return _location;
}