如何将这个JSON文件读入Swift?

时间:2018-11-25 15:39:33

标签: json swift parsing mapbox

我正尝试通过使用实时供稿SDK示例将以下JSON文件绘制到MapBox SDK中。

有人知道我如何将航班号,经度,纬度和飞机类型解析为Swift吗?

我已将链接附加到MapBox JSON文件,新的JSON文件,以及来自SDK的带注释的代码段和Swift代码。

JSON文件的带注释的片段:

Annotated snippet of the JSON file.

https://data-live.flightradar24.com/zones/fcgi/feed.js

https://wanderdrone.appspot.com/

import Mapbox

class LiveDataExample: UIViewController, MGLMapViewDelegate {
    var source: MGLShapeSource!
    var timer = Timer()
    override func viewDidLoad() {
        super.viewDidLoad()

        // Create a new map view using the Mapbox Dark style.
        let mapView = MGLMapView(frame: view.bounds,
                                 styleURL: MGLStyle.darkStyleURL(withVersion: 9))
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        mapView.tintColor = .gray

        // Set the map view‘s delegate property.
        mapView.delegate = self
        view.addSubview(mapView)
    }

    func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
        if let url = URL(string: "https://wanderdrone.appspot.com/") {
            // Add a source to the map. https://wanderdrone.appspot.com/ generates coordinates for simulated paths.
            source = MGLShapeSource(identifier: "wanderdrone", url: url, options: nil)
            style.addSource(source)

            // Add a Maki icon to the map to represent the drone's coordinate. The specified icon is included in the Mapbox Dark style's sprite sheet. For more information about Maki icons, see https://www.mapbox.com/maki-icons/
            let droneLayer = MGLSymbolStyleLayer(identifier: "wanderdrone", source: source)
            droneLayer.iconImageName = NSExpression(forConstantValue: "rocket-15")
            droneLayer.iconHaloColor = NSExpression(forConstantValue: UIColor.white)
            style.addLayer(droneLayer)

            // Create a timer that calls the `updateUrl` function every 1.5 seconds.
            timer.invalidate()
            timer = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(updateUrl), userInfo: nil, repeats: true)
        }
    }

    @objc func updateUrl() {
        // Update the icon's position by setting the `url` property on the source.
        source.url = source.url
    }

    override func viewWillDisappear(_ animated: Bool) {
        // Invalidate the timer if the view will disappear.
        timer.invalidate()
        timer = Timer()
    }
}

0 个答案:

没有答案