【问题标题】:Drawing route direction between two locations using Google Map Swift 4.0 [closed]使用 Google Map Swift 4.0 在两个位置之间绘制路线方向 [关闭]
【发布时间】:2019-02-19 23:27:45
【问题描述】:

我无法将 JSON 响应呈现到地图上。 我正在尝试制作一个您可以在目的地中输入的应用程序,谷歌地图将根据您当前的位置制作路线。

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

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

我在网上看到的一切都过时了

任何帮助都会非常感谢。

如果你能指点我一个教程,那就太好了!

谢谢:)

【问题讨论】:

    标签: swift xcode api google-maps gmsmapview


    【解决方案1】:

    我会这样回答:

      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")
                }
    
    
    
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 2017-03-19
      相关资源
      最近更新 更多