ios - 谷歌地图 2 点之间的路线与多航点 swift ios
<p><p>我有这段代码,但由于某种原因,它只是在 2 个点(第一个点和最后一个点)之间画了一个溃败,忽略了所有其他点,即 </p>
<p>输出:仅在 2 个标记之间路由</p>
<p>预期输出:所有标记之间的路线(5 个标记)</p>
<p>有人知道我的代码有什么问题吗? </p>
<pre><code> func getDotsToDrawRoute(positions : , 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 : = ["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?
else {
return
}
print ("route iss ::: \(dictionary["routes"])")
if let routes = dictionary["routes"] as? [] {
if routes.count > 0 {
var first = routes.first
if let legs = first!["legs"] as? [] {
let fullPath : GMSMutablePath = GMSMutablePath()
for leg in legs {
if let steps = leg["steps"] as? [] {
for step in steps {
if let polyline = step["polyline"] as? {
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 }
}
}
}
}
})
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>看看这个解决方案是否适合我</p>
<pre><code> func drawpath(positions: ) {
let origin = positions.first!
let destination = positions.last!
var wayPoints = ""
for point in positions {
wayPoints = wayPoints.characters.count == 0 ? "\(point.latitude),\(point.longitude)" : "\(wayPoints)%7C\(point.latitude),\(point.longitude)"
}
let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin!.latitude),\(origin!.longitude)&destination=\(destination.latitude),\(destination.longitude)&mode=driving&waypoints=\(wayPoints)&key=KEY"
Alamofire.request(url).responseJSON { response in
print(response.request as Any)// original URL request
print(response.response as Any) // HTTP URL response
print(response.data as Any) // server data
print(response.result as Any) // result of response serialization
let json = try!JSON(data: response.data!)
let routes = json["routes"]["overview_polyline"]["points"].stringValue
let path = GMSPath.init(fromEncodedPath: routes)
let polyline = GMSPolyline.init(path: path)
polyline.strokeWidth = 4
polyline.strokeColor = UIColor.red
polyline.map = self._map
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 谷歌地图 2 点之间的路线与多航点 swift ios,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/53152848/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/53152848/
</a>
</p>
页:
[1]