gpt4 book ai didi

iphone - 在 for 循环中使用不同的 url 多次发送 NSURLConnection 请求

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:57 29 4
gpt4 key购买 nike

我有一个地址数组,需要使用 Google 的 Geocode api 将其转换为纬度/经度。我将一个地址和城市输入到 Google Geocode URL 中,它形成了一个正确的连接 url。

基本上我希望能够使用 for 循环来创建多个 NSURLConnection 请求,返回多个响应。

-(void)setString{
for (int i = 0; i < [businessArray count]; i ++)
{
NSString *address = [addressArray objectAtIndex:0];
NSString *city = [locationDict valueForKey:@"city"];
NSString *geocodeURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@,+%@,&sensor=true", address, city];
geocodeURL = [geocodeURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:geocodeURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10];
NSLog(@"%@", request);
geoCodeConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];


if (geoCodeConnection)
{
responseData = [NSMutableData data];
connectionIsActive = YES;
NSLog(@"connection active");

} else {
NSLog(@"connection failed");
connectionIsActive = NO;
}

}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{


NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError *jsonError = nil;

SBJsonParser *json = [[SBJsonParser alloc] init];

NSDictionary *parsedJSON = [json objectWithString:responseString error:&jsonError];

NSString *lat= [[[[parsedJSON valueForKey:@"results"] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lat"];
NSString *lng= [[[[parsedJSON valueForKey:@"results"] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lng"];

NSLog(@"lat = %@ long= %@", lat, lng);
connectionIsActive = NO;
[geoCodeLatArray addObject:lat];
[geoCodeLngArray addObject:lng];
NSLog(@"geoCodeArrayLat: %@", geoCodeLatArray);


}

现在代码只返回最后一个地址的纬度和经度。如何使用 JSON 发送多重请求并返回多重响应?

最佳答案

试试这个,我正在用这个,

for(int i=0;i< businessArray.count;i++)
{
NSString *address = [addressArray objectAtIndex:i];
NSString *city = [locationDict valueForKey:@"city"];
NSString *address = [NSString stringWithFormat:@"%@,%@", address, city];

CLLocationCoordinate2D location = [self geoCodeUsingAddress:address];
// then here store the location.latitude in lat array and location.longitude in long array.
}

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];

NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];

NSDictionary *resultsDict = [googleResponse valueForKey: @"results"];
NSDictionary *geometryDict = [resultsDict valueForKey: @"geometry"];
NSDictionary *locationDict = [geometryDict valueForKey: @"location"];

NSArray *latArray = [locationDict valueForKey: @"lat"];
NSString *latString = [latArray lastObject];

NSArray *lngArray = [locationDict valueForKey: @"lng"];
NSString *lngString = [lngArray lastObject];

CLLocationCoordinate2D location;
location.latitude = [latString doubleValue];
location.longitude = [lngString doubleValue];

return location;
}

更新上述功能:

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
double latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}

这对我有用。

关于iphone - 在 for 循环中使用不同的 url 多次发送 NSURLConnection 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17519839/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com