我使用GoogleMap Direction API绘制多个位置的路线。我用postman检查响应状态,我可以检查200 OK。但问题是当我使用Alamofire时,响应就是失败!
当我从原点到目的地绘制时,响应是可以的。(总是) 但是如果我添加额外的位置,Alamofire的返回就是失败。
有人有同样的问题要解决吗?
下面是我的代码。感谢名单!
originAddress = locationInfoList[0]
destinationAddress = locationInfoList.last
var wayPointUrl:String = ""
var waypoint = locationInfoList.dropFirst()
waypoint = waypoint.dropLast()
for coordinate in waypoint {
print("~~~~")
let wayPoint = "|\(coordinate.latitude),\(coordinate.longitude)"
wayPointUrl.append(wayPoint)
print("~XXXXXXX~~~")
print(wayPointUrl)
print("XXXXXXX")
}
directionURL = "https://maps.googleapis.com/maps/api/directions/json?" +
"origin=\(originAddress.latitude),\(originAddress.longitude)&destination=\(destinationAddress.latitude),\(destinationAddress.longitude)&waypoints=optimize:true\(wayPointUrl)" +
"&key=apikey"
print("VVVVVVV")
print(directionURL)
print("VVVVVVVV")
}
Alamofire.request(directionURL, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
print(response)
if let JSON = response.result.value {
print(JSON)
let mapResponse: [String: AnyObject] = JSON as! [String : AnyObject]
let routesArray = (mapResponse["routes"] as? Array) ?? []
let routes = (routesArray.first as? Dictionary<String, AnyObject>) ?? [:]
let overviewPolyline = (routes["overview_polyline"] as? Dictionary<String,AnyObject>) ?? [:]
let polypoints = (overviewPolyline["points"] as? String) ?? ""
let line = polypoints
self.addPolyLine(encodedString: line)
}
}
}
答案 0 :(得分:2)
您似乎在URL中发送参数并使用JSON编码。将JSONEncoding.default
更改为URLEncoding.default
以获取Alamofire请求的编码参数。
同样来自Google Map API文档,其内容如下。所以我相信,URL编码将解决您的问题。
每个航路点可以是地名,地址或以逗号分隔的纬度/经度坐标。字符串应该是URL转义的,因此诸如“柏林,德国|巴黎,法国”之类的航点应转换为柏林%2CGermany%7CParis%2CFrance。