gpt4 book ai didi

ios - google maps route between 2 points with multi waypoints swift ios

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:52:55 24 4
gpt4 key购买 nike

我有这段代码,但出于某种原因,它只是在 2 点(第一个和最后一个点)之间画一条路线,忽略所有其他点,即 [index == 1 到 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 }
}
}
}
}
})
}
}

最佳答案

看看这个解决方案是否适合我

    func drawpath(positions: [CLLocationCoordinate2D]) {

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"][0]["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
}


}

关于ios - google maps route between 2 points with multi waypoints swift ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53152848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com