【问题标题】:how to get response from directions API google maps?如何从方向 API 谷歌地图获得响应?
【发布时间】:2017-09-22 04:30:20
【问题描述】:

我正在尝试获取 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,标题 { “缓存控制”=“公共,最大年龄=86400”; “内容编码”= gzip; “内容长度”= 23856; “内容类型”=“应用程序/json;字符集=UTF-8”; 日期 =“2017 年 4 月 25 日星期二 08:48:36 GMT”; Expires = "2017 年 4 月 26 日星期三 08:48:36 GMT"; 服务器 = mafe; Vary = "接受语言"; "alt-svc" = "quic=\":443\"; ma=2592000; v=\"37,36,35\""; “x-frame-options”=同源; “x-xss-保护”=“1;模式=块”; } }

【问题讨论】:

  • 添加您的错误信息。
  • 请检查编辑
  • @HarjotSinghPanesar 您没有收到错误消息,而是成功获得响应,现在您需要从中访问数据和序列化 JSON
  • 这似乎不是错误。您只是在完成块中打印 NSURLResponse(其 HTTP 代码为 200 表示 OK),而不是 data

标签: ios google-maps directions


【解决方案1】:

这样试试

   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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2017-06-08
    • 2015-04-03
    • 2013-03-16
    相关资源
    最近更新 更多