如何在Swift中使用正确的参数从Google Maps请求旅行时间API

我正在尝试从Google距离矩阵中请求旅行时间。我使用了alamofire,但我认为JSON编码不正确且参数格式不正确。

我应该获取网址:

https://maps.googleapis.com/maps/api/distancematrix/json?key=AI2aSyD7pG5IsKRVRcXkXaSRL-6L-a6oD7xGcww&units=imperial&destinations=14.623907,121.063087&origins=12.623907,121.063087

但是当它在alamofire中编码时,我得到:

https://maps.googleapis.com/maps/api/distancematrix?destinations=14.623907%2C%20121.063087&key=AI2aSyD7pG5IsKRVRcXkXaSRL-6L-a6oD7xGcww&origins=55.45887%2C%20101.64401&units=imperial

我猜我的编码方式有问题,因为第一个URL在邮递员中有效。

这是我的代码:

func geteta(originlatitude: String,originLongitude: String,destinationLatitude: String,destinationLongitude: String,APIKey: String) {

    let parameters: Parameters = [
        "units" : "imperial","origins" : "\(originlatitude),\(originLongitude)","destinations" : "\(destinationLatitude),\(destinationLongitude)","key" : "\(APIKey)"
    ]

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

    let distanceMatrixURL = "https://maps.googleapis.com/maps/api/distancematrix"

    Alamofire.request(distanceMatrixURL,method: .get,parameters: parameters,headers: header).responseJSON { response in
        print("response response is \(response.response)")
        print("response request is \(response.request)")
        print("response data is \(response.data)")
        print("response data is \(response.result)")
    }
}
gehui19890302 回答:如何在Swift中使用正确的参数从Google Maps请求旅行时间API

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3060200.html

大家都在问