使用Google Map Swift 4.0在两个位置之间绘制路线方向



我在将JSON响应渲染到地图上遇到了麻烦。我正在尝试制作一个可以在A目的地输入的应用程序,而Google Maps将根据您当前的位置制作路线。

我可以成功打印出控制台中的JSON响应

,但我不确定如何使用polyline制作路线。

我在线查看的所有内容都已过时

任何帮助都非常感谢。

如果您可以将我指向一个教程,那将很棒!

谢谢:)

这就是我的回答方式:

  func drawPath()
    {
        let origin = "(43.1561681),(-75.8449946)"
        let destination = "(38.8950712),(-77.0362758)"

        let url = "https://maps.googleapis.com/maps/api/directions/json?origin=(origin)&destination=(destination)&mode=driving&key=API_KEY"

        Alamofire.request(url).responseJSON { response in
            print(response.request!)  // original URL request
            print(response.response!) // HTTP URL response
            print(response.data!)     // server data
            print(response.result)   // result of response serialization
            do {
                let json = try 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.init(path: path)
                    polyline.map = self.MapView
                }
            }
            catch {
                print("ERROR: not working")
            }

        }
    }

相关内容

最新更新