【问题标题】:Unable to get response from Google distance matrix api无法从 Google 距离矩阵 api 获得响应
【发布时间】:2018-08-15 05:09:18
【问题描述】:

我正在使用谷歌距离矩阵 API,我正在使用以下代码

        let headers: HTTPHeaders = [ "Accept": "application/json", "Content-Type": "application/json" ]

        let url = "https://maps.googleapis.com/maps/api/distancematrix/json?&origins=\(start)&destinations=\(end)&key=AIzaSyDvt_KiUCtdb1kPEw4E4Dt68EuiF8PosAg"

        let header: HTTPHeaders = [ "Accept": "application/json", "Content-Type": "application/json" ]

         Alamofire.request( url, method: .get, encoding: JSONEncoding.default, headers : header) .responseString {  response in

            print(response.request)  // original URL request
            print(response.response) // HTTP URL response
            print(response.data)     // server data
            print(response.result)


        }

根据这个Unable to fetch Response For Google Distance matrix in Swift 我正在传递标题,但我仍然收到以下错误。

这是我的网址,带开头和结尾 "https://maps.googleapis.com/maps/api/distancematrix/json?&origins=Nanpura, Surat, Gujarat 395008, India&destinations=Adajan, Surat, Gujarat, India&key=AIzaSyDvt_KiUCtdb1kPEw4E4Dt68EuiF8PosAg"

【问题讨论】:

  • 检查您的开始和结束变量。如果不为零
  • 你能给我你的开始和结束的值吗?
  • @MayankJain 请检查我的更新代码
  • 我检查了,它工作正常
  • @KhushbuDesai 您的网址包含空格,因此您需要进行编码

标签: ios google-maps-api-3 swift4 google-distancematrix-api


【解决方案1】:

我检查了请求,它工作正常。我猜你在 info.plist 中缺少隐私设置

【讨论】:

  • 不,我已经在 info.plist 中添加了隐私设置,我的开始名和结束名包含一些空格,所以它是否正确?
【解决方案2】:

你应该编码 url,试试这个

    let url = "https://maps.googleapis.com/maps/api/distancematrix/json?&origins=Nanpura, Surat, Gujarat 395008, India&destinations=Adajan, Surat, Gujarat, India&key=AIzaSyDvt_KiUCtdb1kPEw4E4Dt68EuiF8PosAg"
    let encodedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

    let header: HTTPHeaders = [ "Accept": "application/json", "Content-Type": "application/json" ]

    Alamofire.request(encodedUrl! , method: .get,encoding: JSONEncoding.default, headers: header)
        .responseJSON { (data) in
            print(data)
    }

这也可以,

Alamofire.request(encodedUrl!, method: .get,encoding: JSONEncoding.default, headers: header)
            .responseString {response in
                print(response.request)  // original URL request
                print(response.response) // HTTP URL response
                print(response.data)     // server data
                print(response.result)
        }

会有这样的反应,

SUCCESS: {
    "destination_addresses" =     (
        "Adajan, Surat, Gujarat, India"
    );
    "origin_addresses" =     (
        "Nanpura, Surat, Gujarat 395008, India"
    );
    rows =     (
                {
            elements =             (
                                {
                    distance =                     {
                        text = "2.4 km";
                        value = 2433;
                    };
                    duration =                     {
                        text = "6 mins";
                        value = 373;
                    };
                    status = OK;
                }
            );
        }
    );
    status = OK;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2017-12-18
    • 2012-03-27
    相关资源
    最近更新 更多