如何从geoJson文件中提取值并将值传递给iOS Swift中的tableview [英] How to extract values from geoJson file and pass the value to tableview in iOS swift

查看:429
本文介绍了如何从geoJson文件中提取值并将值传递给iOS Swift中的tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取geoJson值并将其存储到一个数组中,并在表视图中列出值的数组以在mapview中显示路线. 在这里,我将Mapbox用于Mapview,因为它支持室内导航.现在,我可以直接从geoJson文件在mapview中显示路线.但是我需要将值提取到数组中以便在tableview中列出值.

I'm trying to extract geoJson values and store it to an array and listing array of values in a tableview to show the route in mapview. Here I am using Mapbox for mapview because it supports indoor navigation. Right now I am able to show routes in the mapview directly from geoJson file. But I need to extract values to an array for listing values in a tableview.

这是我尝试提取geoJson值的代码:

Here is the code I tried for extracting geoJson value:

结构:

struct Collection : Decodable {
    let type : String
    let features : [Feature]
}

struct Feature : Decodable {
    let type : String
    let properties : Properties
    let geometry : Geometry
    // there is also geometry
}

struct Properties : Decodable {
    let name : String
}

struct Geometry : Decodable{
    let coordinates: [CLLocationCoordinate2D]
}

以下是用于加载geoJson文件的代码:

Here is the code used for loading geoJson file:

 func loadGeoJson() {
    DispatchQueue.global().async {
        // Get the path for example.geojson in the app’s bundle  tbiMapbox.
        guard let jsonUrl = Bundle.main.url(forResource: "newData", withExtension: "geojson") else { return }
        guard let jsonData = try? Data(contentsOf: jsonUrl) else { return }
        DispatchQueue.main.async {
            self.drawPolyline(geoJson: jsonData)
            print("data::\(jsonData)")
        }
    }
}

此功能使用绘制折线作为坐标:

This function used drawing polylines for coordinates:

func drawPolyline(geoJson: Data) {
    guard let style = self.mapView.style else { return }

    let shapeFromGeoJSON = try! MGLShape(data: geoJson, encoding: String.Encoding.utf8.rawValue)
    let source = MGLShapeSource(identifier: "polyline", shape: shapeFromGeoJSON, options: nil)
    style.addSource(source)

    let layer = MGLLineStyleLayer(identifier: "polyline", source: source)

    layer.lineJoin = NSExpression(forConstantValue: "round")
    layer.lineCap = NSExpression(forConstantValue: "round")

    layer.lineColor = NSExpression(forConstantValue: UIColor(red: 59/255, green:178/255, blue:208/255, alpha:1))


    layer.lineWidth = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)",
                                   [14: 2, 18: 20])

    let casingLayer = MGLLineStyleLayer(identifier: "polyline-case", source: source)
    casingLayer.lineJoin = layer.lineJoin
    casingLayer.lineCap = layer.lineCap
    casingLayer.lineGapWidth = layer.lineWidth
    casingLayer.lineColor = NSExpression(forConstantValue: UIColor(red: 41/255, green:145/255, blue:171/255, alpha:1))
    casingLayer.lineWidth = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)",
                                         [14: 1, 18: 4])

    let dashedLayer = MGLLineStyleLayer(identifier: "polyline-dash", source: source)
    dashedLayer.lineJoin = layer.lineJoin
    dashedLayer.lineCap = layer.lineCap
    dashedLayer.lineColor = NSExpression(forConstantValue: UIColor.red)
    dashedLayer.lineOpacity = NSExpression(forConstantValue: 0.5)
    dashedLayer.lineWidth = layer.lineWidth
    dashedLayer.lineDashPattern = NSExpression(forConstantValue: [0, 1.5])

    style.addLayer(layer)
    style.addLayer(dashedLayer)
    style.insertLayer(casingLayer, below: layer)
}

对于表视图,现在我正在加载静态数据:

For table view, right now i am loading static data:

var nameArray = [String]()
var coordinatesArray = [CLLocationCoordinate2D]()

var nameARR = ["1", "2", "3"]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nameARR.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! newTableViewCell

    cell.companyName.text = nameARR[indexPath.row]
    return cell
}

我使用的geoJson值:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "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
          ]
        ]
      }
    }
  ]
}

我的问题是1.)如何提取geoJson文件的值? 2.)如何将提取的值传递给tableview?在点击tableview单元格时,它会执行segue并传递受尊重的坐标索引.

My question is 1.) How to extract values geoJson file? 2.) How to pass the extracted values to the tableview?, when tapping tableview cell, it perform segue and also pass the respected index of coordinates too.

推荐答案

您可以使用此结构来解析您发布的JSON:

You can use this struct to parse the JSON you've posted:

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

并使用类似的内容进行解析

and parse it with something like

guard let jsonUrl = Bundle.main.url(forResource: "newData", withExtension: "geojson") else { return }
guard let jsonData = try? Data(contentsOf: jsonUrl) else { return }
do {
   let geoData = try JSONDecoder().decode(Geodata.self, from: jsonData)
} catch { 
   print("\(error)") 
}

这篇关于如何从geoJson文件中提取值并将值传递给iOS Swift中的tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆