首页 > 解决方案 > 如何使用多个航点swift4绘制路线

问题描述

我想使用开始位置和结束位置在多个点之间绘制路线,我正在按照一些示例和此链接获取 json 以获取航点 https://directionsdebug.firebaseapp.com/?origin=place_id%3AChIJ59dGBGBwTDkRlQsbxkBkqNw&destination=place_id%3AChIJ0TGAdgh6TDkRTFcRvOIXIOY&mode=驾驶&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
            }


    }

标签: iosswiftgoogle-mapsdirectiongoogle-directions-api

解决方案


我在确定要创建的折线时发现错误,您应该使用响应中的“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
    }

我得到的输出:

在此处输入图像描述

希望能有所帮助。


推荐阅读