如何使用 Swift 3 在 Apple 地图中绘制 GeoJSON 作为叠加层
How to draw GeoJSON in Apple Maps as overlay using Swift 3
我正在尝试将以下 GeoJSON 数据绘制为 MKMapView 中的覆盖图。
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 可用于执行此操作。
我在Swift3中写了如下代码
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 {
}
}
}
但这不起作用。
在 features
的第一个字典中,您的 coordinates
键具有二维数组,因此您还需要在 geometryObject.values
for 循环中再添加一个 if let
条件。
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, properties: properties!, geometry: geometries!)
features.append(feature)
我正在尝试将以下 GeoJSON 数据绘制为 MKMapView 中的覆盖图。
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 可用于执行此操作。
我在Swift3中写了如下代码
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 {
}
}
}
但这不起作用。
在 features
的第一个字典中,您的 coordinates
键具有二维数组,因此您还需要在 geometryObject.values
for 循环中再添加一个 if let
条件。
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, properties: properties!, geometry: geometries!)
features.append(feature)