如何从geoJson附加2D数组?

时间:2018-10-10 03:36:33

标签: ios arrays json swift struct

我有用于显示室内导航地图的geojson文件。从geojson响应中,我得到的是二维数组的坐标。这是我的JSON响应:

{
  "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
          ]
        ]
      }
    }
  ]
}

在这里,我尝试将二维坐标附加到数组中。使用坐标数组在AR(增强现实)中显示折线。这是到目前为止我尝试过的代码:

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()
}

这是我的结构体类:

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?
}

我的问题是在追加数组后,当我将数组索引指定为0时,它会打印一个lat而不是2d数组。我得到的这个:

[80.2226534485817,
12.871137160770251] 

但是我想要的是

 [[[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]]]

打印数组索引0时,预期结果是

 [[80.2226534485817, 12.871137160770251], [80.22263333201408, 
 12.871145658917484], [80.22264339029789, 12.871184881131773], [80.2225998044014, 
 12.871194686684378], [80.22260718047619, 12.87121625889878]]

已引用此链接How to draw GeoJSON in Apple Maps as overlay using Swift 3,但未能成功。.

1 个答案:

答案 0 :(得分:1)

您需要如下声明coordinatesArray,然后进行相应分配,

// Declaration
var coordinatesArray: [[[Double]]] = []
// Assignment
coordinatesArray = baseData.features.map({ $0.geometry.coordinates })
print(coordinatesArray[0])

输出

[[80.222653448581696, 12.871137160770251], [80.222633332014084, 12.871145658917484], [80.22264339029789, 12.871184881131773], [80.222599804401398, 12.871194686684378], [80.222607180476189, 12.87121625889878]]