无法从 GEOSwift 库解析几何 LineString 以在 MapView 中显示折线

Not able to parse the geometry LineString from GEOSwift library to show polyline in MapView

我有 geojson 文件要在 Mapview 中显示。它包含“LineString”类型下的数千个坐标。因此,为了解析,我使用了“GEOSwift”库。

但是,在解析时间时,它无法从 LineString 获取数据,并且无法从以下条件中获取数据。

                    if let item = item as? LineString {

密码是

DispatchQueue.global(qos: .background).async {
            if let geoJSONURL = Bundle.main.url(forResource: "LineString", withExtension: "geojson") {
                do {
                    var overlays = [MKPolyline]()
                    let features = try Features.fromGeoJSON(geoJSONURL)
                    for item in features! {
                        if let item = item as? LineString {
                            let polyLine = item.mapShape() as! MKPolyline
                            overlays.append(polyLine)
                        }
                    }
                    DispatchQueue.main.async {
                        // add overlays to map
                        self.mapView.addOverlays(overlays)
                    }

                } catch {

                }
            }
        }

甚至像下面这样尝试,但是,显示编译时错误。

let geometryDict = item["geometry"]
let lineStringType = geometryDict["type"]

geojson的样本数据如

{"type":"FeatureCollection”,”features":[{"type":"Feature","id":1,"geometry":{"type":"LineString","coordinates":[[-61.4127132774969,42.9804121510986],[-61.412698736004,62.9807528172026],[-61.4126676674304,42.9808383428383]]},{"type":"Feature","id":2,"geometry":{"type":"LineString","coordinates":[[-61.4124601404427,32.9810257092771],[-61.4124646891238,32.9810320381762],[-61.412690615116,32.9813462742651]]}

层次结构就像 PFA。

谁能告诉我哪里做错了?

我完全遵循了link,但是,他们没有给出LineString的例子。

https://github.com/GEOSwift/GEOSwift

通过变量检查器,我发现您访问元素的方式有误。使用下一个表达式访问 features 序列中的第一个 LineString

if let item = item.geometries[0] as? LineString {