【问题标题】:how to draw route using multiple waypoints swift4如何使用多个航点swift4绘制路线
【发布时间】:2018-06-20 10:31:41
【问题描述】:

我想使用起始位置和结束位置在多个点之间绘制路线,我正在关注一些示例和此链接以获取 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
            }


    }

【问题讨论】:

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


【解决方案1】:

试试这个代码可能对你有帮助(源点和目的地点之间的路线)

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

        }
    }

【讨论】:

    【解决方案2】:

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

    所以我对你所做的代码做了一点改动:

    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
        }
    

    我得到的输出:

    希望能帮上忙。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-29
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      相关资源
      最近更新 更多