如何使用多个航点绘制路线swift4

时间:2018-06-20 10:31:41

标签: ios swift google-maps direction google-directory-api

我想使用开始位置和结束位置在多个点之间绘制路线,我在跟踪一些示例,并且此链接获取json以获取航点 https://directionsdebug.firebaseapp.com/?origin=place_id%3AChIJ59dGBGBwTDkRlQsbxkBkqNw&destination=place_id%3AChIJ0TGAdgh6TDkRTFcRvOIXIOY&mode=driving&waypoints=Kotri%2C%20Jamshoro%2C%20Sindh%2C%20Pakistan&alternatives=true

这是我的代码,我没有使用链接获取所有要点,请引导我如何使用起点和航路点

func drawpath() {

        let url = "https://maps.googleapis.com/maps/api/directions/json?origin=place_id%3AChIJ59dGBGBwTDkRlQsbxkBkqNw&destination=place_id%3AChIJ0TGAdgh6TDkRTFcRvOIXIOY&mode=driving&waypoints=Kotri%2C%20Jamshoro%2C%20Sindh%2C%20Pakistan&alternatives=true&key=AIzaSyCct09KdoyIc3VV5Bziw5Tk9MF0RhWXTNE"
        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]["legs"][0]["steps"].arrayValue
            print("route is\(routes)")
            // print route using Polyline
            for route in routes
            {
                let routeOverviewPolyline = route["polyline"].dictionary
                let points = routeOverviewPolyline?["points"]?.stringValue
                print("ppoint\(String(describing: points))")
                print("routeOverviewPolyline\(String(describing: routeOverviewPolyline))")
                let path = GMSPath.init(fromEncodedPath: points!)
                let polyline = GMSPolyline.init(path: path)
                polyline.strokeWidth = 4
                polyline.strokeColor = UIColor.red
                polyline.map = self.mapview
            }


    }

2 个答案:

答案 0 :(得分:0)

尝试此代码可能对您有帮助(源点和目标点之间的路线)

let origin = "\(37.778483),\(-122.513960)"
    let destination = "\(37.706753),\(-122.418677)"
    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving&key=[YOUR-API-KEY]"

    Alamofire.request(url).responseJSON { response in
        let json = JSON(data: response.data!)
        let routes = json["routes"].arrayValue

        for route in routes
        {
            let routeOverviewPolyline = route["overview_polyline"].dictionary
            let points = routeOverviewPolyline?["points"]?.stringValue
            let path = GMSPath.init(fromEncodedPath: points!)

            let polyline = GMSPolyline(path: path)
            polyline.strokeColor = .black
            polyline.strokeWidth = 10.0
            polyline.map = mapViewX

        }
    }

答案 1 :(得分:0)

我在确定要创建的折线时发现错误,您应该使用响应中的“ overview_polyline”对象

enter image description here

所以我对您编写的代码做了些微改动:

func drawpath() {

        let url = "https://maps.googleapis.com/maps/api/directions/json?origin=place_id%3AChIJ59dGBGBwTDkRlQsbxkBkqNw&destination=place_id%3AChIJ0TGAdgh6TDkRTFcRvOIXIOY&mode=driving&waypoints=Kotri%2C%20Jamshoro%2C%20Sindh%2C%20Pakistan&alternatives=true&key=AIzaSyCct09KdoyIc3VV5Bziw5Tk9MF0RhWXTNE"
        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.mapview
    }

我得到的输出:

enter image description here

希望能为您提供帮助。