无法绘制折线 Google 地图:Swift 3

Not able to draw Polyline Google Maps : Swift 3

我正在尝试在两点之间绘制路线,但无法这样做,因为我的多点中没有值。 我就是这样做的:

正在解析 JSON:

if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{

                        if let routes = json["routes"] as? [Any]{
                            if let overview_polyline = routes[0] as?[String:Any]{
                                print("overview_polyline\(overview_polyline)")// Getting values till here
                                if let polyString = overview_polyline["points"] as? String{

                                    //Call this method to draw path on map
                                    self.showPath(polyStr: polyString)
                                }

                            }
                        }
                    }

直到 polystring 我得到了值,但没有得到这行代码的任何值 if let polyString = overview_polyline["points"] as? String。 知道为什么 polyStringnil 吗?

我已经完成了这个 link 以清除仍然无法实现的概念。

我通过将上面的代码修改为这段代码来实现的。所以要解析 google api 并在地图上创建折线,您可以使用此代码,写在 swift 3.

if let json : [String:Any] = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]{
      let routes = json["routes"] as! NSArray;
      print(routes)
      self.mapView.clear()
      OperationQueue.main.addOperation({
          for route in routes {
                let routeOverviewPolyline:NSDictionary = (route as! NSDictionary).value(forKey: "overview_polyline") as! NSDictionary
                let points = routeOverviewPolyline.object(forKey: "points")
                let path = GMSPath.init(fromEncodedPath: points! as! String)
                let polyline = GMSPolyline.init(path: path)
                polyline.strokeWidth = 3
                polyline.map = self.mapView
           }
       })
}