【问题标题】:Not able to draw Polyline Google Maps : Swift 3无法绘制折线谷歌地图:Swift 3
【发布时间】:2018-07-19 14:18:28
【问题描述】:

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

解析 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 吗?

我已经通过这个链接来清除仍然无法实现它的概念。 unexpectedly found nil while unwrapping an Optional value

【问题讨论】:

  • 您是否验证了overview_polyline["points"] 是字符串而不是其他数据类型?

标签: ios google-maps swift3 google-polyline


【解决方案1】:

我通过将上面的代码修改为此代码来实现这一点。所以要解析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
           }
       })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-16
    • 2016-05-14
    • 1970-01-01
    • 2013-07-02
    • 2023-04-06
    • 2017-11-01
    • 2018-03-06
    • 1970-01-01
    相关资源
    最近更新 更多