ios – 如何使用Swift 3在Apple Maps中绘制GeoJSON作为叠加层

ios – 如何使用Swift 3在Apple Maps中绘制GeoJSON作为叠加层,第1张

概述我试图在MKMapView中过度绘制以下Geo JSON数据. GeoJSON文件如下. {"type": "FeatureCollection","features": [ { "type": "Feature", "properties": { "Name": "line1" }, 我试图在MKMapVIEw中过度绘制以下Geo JSON数据.

GeoJsON文件如下.

{"type": "FeatureCollection","features": [    {        "type": "Feature","propertIEs": {            "name": "line1"        },"geometry": {            "type": "linestring","coordinates": [                [                    76.35498046875,25.145284610685064                ],[                    81.36474609375,25.06569718553588                ],[                    83.91357421875,23.301901124188877                ],[                    82.001953125,22.004174972902003                ],[                    78.33251953125,21.248422235627014                ],[                    76.31103515625,21.268899719967695                ]            ]        }    },{        "type": "Feature","propertIEs": {            "name": "point8"        },"geometry": {            "type": "Point","coordinates": [                74.50927734375,20.076570104545173            ]        }    }]

}

但我无法解析它.请帮忙.是否有任何第三方SDK可用于执行此 *** 作.

我已经在Swift 3中编写了如下代码

func loadJsON() {    var features = [Feature]()    guard let url = Bundle.main.url(forResource: "Geo",withExtension: "Json") else {        return    }    do {        let data = try Data(contentsOf: url)        guard let rootObject = try JsONSerialization.JsonObject(with: data,options: .allowFragments) as? [String : Any]  else {            return        }        guard let featureObjects = rootObject["features"] as? [[String: AnyObject]] else {            return        }        for feature in featureObjects {            if let type = feature["type"] as? String,let propertyObject = feature["propertIEs"] as? [String : String],let geometryObject = feature["geometry"] as? [String : AnyObject] {                var propertIEs: PropertyVal?                for property in propertyObject.values {                    propertIEs = PropertyVal(name: property)                }                var geometrIEs: Geometry?                var coordData: [Coordinates]?                var coord: Coordinates                var typeData: String = ""                for obj in geometryObject.values {                    print(obj)                    if let type = obj as? String {                        typeData = type                    } else {                        //return                    }                    if let coordobj = obj as? [Double] {                        coord = Coordinates(latitude: coordobj[0],longitude: coordobj[1])                        coordData?.append(coord)                    } else {                        //return                    }                    geometrIEs = Geometry(type: typeData,coordinates: coordData!)                }                let feature = Feature(type: type,propertIEs: propertIEs!,geometry: geometrIEs!)                features.append(feature)                print("Feature is \(features)")            }        }    } catch {    }}

}

但这不起作用.

解决方法 在第一个特征字典中,您的坐标键具有2D数组,因此您还需要在geometryObject.values for循环中添加一个if条件.

for obj in geometryObject.values {    print(obj)    if let type = obj as? String {        typeData = type    }     if let coordobj = obj as? [Double] {        coord = Coordinates(latitude: coordobj[0],longitude: coordobj[1])        coordData?.append(coord)    }    //Add one more if let condition    if let coordobj = obj as? [[Double]] {         for coordinate in coordobj {            let coord = Coordinates(latitude: coordinate[0],longitude: coordinate[1])            coordData?.append(coord)        }    }    geometrIEs = Geometry(type: typeData,coordinates: coordData!)}let feature = Feature(type: type,geometry: geometrIEs!)features.append(feature)
总结

以上是内存溢出为你收集整理的ios – 如何使用Swift 3在Apple Maps中绘制GeoJSON作为叠加层全部内容,希望文章能够帮你解决ios – 如何使用Swift 3在Apple Maps中绘制GeoJSON作为叠加层所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1032149.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存