OStack程序员社区-中国程序员成长平台

标题: ios - 谷歌地图 2 点之间的路线与多航点 swift ios [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:59
标题: ios - 谷歌地图 2 点之间的路线与多航点 swift ios

我有这段代码,但由于某种原因,它只是在 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                                }
                        }
                    }
                }
            }
        })
    }
}



Best Answer-推荐答案


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

    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 - 谷歌地图 2 点之间的路线与多航点 swift ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53152848/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4