将多个坐标传递给maps.googleapis.com

时间:2014-03-19 16:17:26

标签: ios iphone objective-c xcode google-maps

我想使用Xcode项目中的Google Maps API检索某些坐标的高程。 我可以检索一个点的数据,但是当我尝试在url请求中传递多个坐标时,我收到以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

我的代码如下:

-(void) queryGooglePlaces {

    //WORKS FINE PASSING ONLY ONE POINT --->
    NSString *url = @"http://maps.googleapis.com/maps/api/elevation/json?locations=36.578581,-118.291994&sensor=false";

    //FAIL WHEN I PASS MORE THAN ONE POINT --->
    //NSString *url = @"http://maps.googleapis.com/maps/api/elevation/json?locations=36.578581,-118.291994|36.23998,-116.83171&sensor=false";

    //Formulate the string as a URL object.
    NSURL *googleRequestURL=[NSURL URLWithString:url];

    // Retrieve the results of the URL.
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

-(void)fetchedData:(NSData *)responseData {
    NSError* error;

    //THE CODE CRASHES HERE ---->
    NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData
                      options:kNilOptions
                      error:&error];

    NSArray* places = [json objectForKey:@"results"];  
}

我不知道问题是什么。根据Google的指南,语法看起来是正确的。

感谢。

1 个答案:

答案 0 :(得分:0)

我自己回答,

在url变量声明下面添加以下语句,问题已解决:

url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

我认为角色管道产生了问题,使用此指令通过添加百分比转义来编码NSString以提供有效的URL。

祝你好运!