【发布时间】:2019-04-08 17:18:53
【问题描述】:
我有这段代码,但由于某种原因,它只是在 2 个点(第一个点和最后一个点)之间画了一个溃败,忽略了所有其他点,即 [index == 1 to index == n-1]
输出:仅在 2 个标记之间路由
预期输出:所有标记之间的路线(5 个标记)
有人知道我的代码有什么问题吗?
func getDotsToDrawRoute(positions : [CLLocationCoordinate2D], completion: @escaping(_ path : GMSPath) -> Void) {
if positions.count > 1 {
let origin = positions.first
let destination = positions.last
var wayPoints = ""
for point in positions {
wayPoints = wayPoints.characters.count == 0 ? "\(point.latitude),\(point.longitude)" : "\(wayPoints)|\(point.latitude),\(point.longitude)"
}
print("this is fullPath :: \(wayPoints)")
let request = "https://maps.googleapis.com/maps/api/directions/json"
let parameters : [String : String] = ["origin" : "\(origin!.latitude),\(origin!.longitude)", "destination" : "\(destination!.latitude),\(destination!.longitude)", "wayPoints" : wayPoints, "stopover": "true", "key" : kyes.google_map]
Alamofire.request(request, method:.get, parameters : parameters).responseJSON(completionHandler: { response in
guard let dictionary = response.result.value as? [String : AnyObject]
else {
return
}
print ("route iss ::: \(dictionary["routes"])")
if let routes = dictionary["routes"] as? [[String : AnyObject]] {
if routes.count > 0 {
var first = routes.first
if let legs = first!["legs"] as? [[String : AnyObject]] {
let fullPath : GMSMutablePath = GMSMutablePath()
for leg in legs {
if let steps = leg["steps"] as? [[String : AnyObject]] {
for step in steps {
if let polyline = step["polyline"] as? [String : AnyObject] {
if let points = polyline["points"] as? String {
fullPath.appendPath(path: GMSMutablePath(fromEncodedPath: points))
}
}
}
let polyline = GMSPolyline.init(path: fullPath)
polyline.path = fullPath
polyline.strokeWidth = 4.0
polyline.map = self._map }
}
}
}
}
})
}
}
【问题讨论】:
-
waypoint 变量应该是一个字符串数组,其中每个字符串应该是位置的纬度经度,用逗号分隔。
-
它已经这样,这里是航点的准备后的列表:24.7618090308257,46.605559787157 | 24.75161673279,46.6651155058871 | 24.7811909269468,46.6336618042914 | 24.7497328878959,46.6820742483448 | 24.7102263535113,46.6874250662913 | 24.7845409226333,46.6233112442547 @AravindBhuvanendran SPAN >
-
不是在alamofire中单独发送参数,你能不能用参数创建一个url,然后像这样调用Get方法maps.googleapis.com/maps/api/directions/json?origin=orglat, orglong&destination=destlat, destlong2&waypoints=waypointlat1, waypointlong1|waypointlat2, waypointlong2&mode=驾驶&key=GoogleKey
-
我只是更新你的网址并尝试一下,结果是:“致命错误:'试试!'表达式意外引发错误:Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}:" @AravindBhuvanendran
-
你能把你的网址不带地图键吗?
标签: ios google-maps swift3