1

我有 60 个地址,我想为每个地址添加引脚。我想使用谷歌地图而不是苹果地图。我在外面有一个循环,并为 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;

}
4

0 回答 0