如何从方向API谷歌地图获得响应?

时间:2017-04-25 08:53:56

标签: ios google-maps directions

我正在尝试获取Google Directions API的回复。你能找到这个错误吗?

NSString *str = [NSString stringWithFormat:@"%@origin=%f,%f&destination=%f,%f&key=%@",@"https://maps.googleapis.com/maps/api/directions/json?",
                     self.marker.position.latitude,
                     self.marker.position.longitude,
                     self.SecondMarker.position.latitude,
                     self.SecondMarker.position.longitude,
                     @"AIzaSyDhZvxehd6ZDKFOB67WIyeLpy7KITwRPw0"];
    //str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //NSString *params = @"";
    [request setHTTPMethod:@"POST"];
    //[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithRequest:request
                completionHandler:^(NSData *data,
                                    NSURLResponse *response,
                                    NSError *error) {
                    // handle response
                    if (!error && [data length]) {
                        NSLog(@"Reponse: %@",response);
                    }
                    else{
                        NSLog(@"%@",error.description);
                    }
                }] resume];

请建议一些代码行?

错误讯息:

Reponse: <NSHTTPURLResponse: 0x60800002d480> { URL: https://maps.googleapis.com/maps/api/directions/json?origin=-33.860000,151.200000&destination=-34.224142,150.068408&key=AIzaSyDhZvxehd6ZDKFOB67WIyeLpy7KITwRPw0
     

} {状态代码:200,headers {           &#34;缓存控制&#34; =&#34; public,max-age = 86400&#34 ;;           &#34;内容编码&#34; = gzip;           &#34; Content-Length的&#34; = 23856;           &#34;内容类型&#34; =&#34; application / json;字符集= UTF-8&#34 ;;           日期=&#34;星期二,2017年4月25日08:48:36 GMT&#34 ;;           Expires =&#34; Wed,26 Apr 2017 08:48:36 GMT&#34 ;;           Server = mafe;           Vary =&#34; Accept-Language&#34 ;;           &#34; ALT-SVC&#34; =&#34; quic = \&#34;:443 \&#34 ;; MA = 2592000; V = \&#34; 37,36,35 \&#34;&#34 ;;           &#34; X框选项&#34; = SAMEORIGIN;           &#34; X-XSS-保护&#34; =&#34; 1;模式=块&#34 ;;       }}

1 个答案:

答案 0 :(得分:0)

试试这个

   NSString *str = [NSString stringWithFormat:@"%@origin=%f,%f&destination=%f,%f&key=%@",@"https://maps.googleapis.com/maps/api/directions/json?",
                     self.marker.position.latitude,
                     self.marker.position.longitude,
                     self.SecondMarker.position.latitude,
                     self.SecondMarker.position.longitude,
                     @"AIzaSyDhZvxehd6ZDKFOB67WIyeLpy7KITwRPw0"];

str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {

                if (!error && [data length])
                {
                    NSLog(@"Reponse: %@",response);

                    NSError *e = nil;
                    NSDictionary *JSONarray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:&e];
                    NSLog(@"Response dictionary is%@",JSONarray);
                }
                else
                {
                    NSLog(@"%@",error.description);
                }
            }] resume];