无法从 Google 距离矩阵 api 获得响应

Unable to get response from Google distance matrix api

我正在使用 google 距离矩阵 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 i am passing header but still i am getting following error.

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

你应该编码 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;
}

我检查了请求,它工作正常。我猜你缺少 info.plist

中的隐私设置