¿Cómo agregar una matriz dimensional 2D en iOS Swift?
Aquí estoy usando el archivo geojson para obtener una matriz 2D para dibujar líneas. No pude agregar dimensiones 2D, al agregarlas se convierte en una matriz dimensional única.
aquí está la salida, que tengo:
coordArray==[[80.2226534485817, 12.871137160770251], [80.22263333201408,
12.871145658917484], [80.22264339029789, 12.871184881131773], [80.2225998044014,
12.871194686684378], [80.22260718047619, 12.87121625889878], [80.22256962954998, 12.87123848481471], [80.22255957126617,
12.871204819088353], [80.22259946912527, 12.871195013536129],
[80.22264305502176, 12.871184881131773], [80.22263266146183,
12.871145658917484], [80.22265445441008, 12.871135526511145]]
Aquí hay una estructura, que he usado:
struct Geodata: Codable {
let type: String
let features: [Feature]
}
struct Feature: Codable {
let type: String
let properties: Properties
let geometry: Geometry
}
struct Geometry: Codable {
let type: String
let coordinates: [[Double]]
}
struct Properties: Codable {
let name: String?
}
Aquí está la variable que he declarado para tableview. Here I have declared two called
nameArrayand
coordenadas Arraywhich I need to use in
tableview`.
var nameArray = [String]()
var coordinatesArray = [[Double]]()
Aquí está el código para extraer el valor geojson:
func loadGeoJson() {
guard let jsonUrl = Bundle.main.url(forResource: "map1", withExtension: "geojson") else { return }
guard let jsonData = try? Data(contentsOf: jsonUrl) else { return }
self.drawPolyline(geoJson: jsonData)
URLSession.shared.dataTask(with: jsonUrl) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
do {
//Decode retrived data with JSONDecoder and assing type of Article object
let baseData = try JSONDecoder().decode(Geodata.self, from: data)
for featu in baseData.features {
self.nameArray.append(featu.properties.name!)
print("nameArray==\(self.nameArray)")
}
for coor in baseData.features {
self.coordinatesArray.append(contentsOf: coor.geometry.coordinates)
print("new coor::\(coor.geometry.coordinates)")
print("coordArray==\(self.coordinatesArray)")
}
} catch let jsonError {
print(jsonError)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}.resume()
}
aquí está mi archivo geojson:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Dot2Globe - CZSM"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
80.2226534485817,
12.871137160770251
],
[
80.22263333201408,
12.871145658917484
],
[
80.22264339029789,
12.871184881131773
],
[
80.2225998044014,
12.871194686684378
],
[
80.22260718047619,
12.87121625889878
]
]
}
},
{
"type": "Feature",
"properties": {
"name": "Entrance - CZSM"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
80.22256962954998,
12.87123848481471
],
[
80.22255957126617,
12.871204819088353
],
[
80.22259946912527,
12.871195013536129
],
[
80.22264305502176,
12.871184881131773
],
[
80.22263266146183,
12.871145658917484
],
[
80.22265445441008,
12.871135526511145
]
]
}
}
]
}