如何设置MGLRasterTileSource的动画

时间:2019-02-27 18:37:42

标签: mapbox mapbox-ios

我正在使用第三方瓷砖API显示天气雷达瓷砖。它的格式为http://example.com/2019-02-22T06:20:00/{z}/{x}/{y}.png

let source = MGLRasterTileSource(identifier: "weather", tileURLTemplates: ["http://example.com/2019-02-22T06:20:00/{z}/{x}/{y}.png"], options: [ .tileSize: 256 ])
let rasterLayer = MGLRasterStyleLayer(identifier: "weather", source: source)
style.addSource(source)
style.addLayer(rasterLayer)

现在我想通过每x秒更改一次图层来设置一系列日期作为动画。

let times = [
    "2019-02-27T16:35:00",
    "2019-02-27T16:40:00",
    "2019-02-27T16:45:00",
    "2019-02-27T16:50:00",
    "2019-02-27T16:55:00"
]

我当前的解决方案是添加一个具有与每个time对应的图层的源,并仅显示当前帧的图层

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
    // Add a new raster source and layer.

    for time in times {
        let identifier = "frame_\(time)"
        source = MGLRasterTileSource(identifier: identifier, tileURLTemplates: ["http://example.com/\(time)/{z}/{x}/{y}.png"], options: [ .tileSize: 256 ])
        let rasterLayer = MGLRasterStyleLayer(identifier: identifier, source: source)
        style.addSource(source)
        style.addLayer(rasterLayer)

        // rasterLayer.isVisible = false
        // workaround
        rasterLayer.rasterOpacity = NSExpression(forConstantValue: 0.01)
    }
    timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { timer in
        self.loop()
    }
}

func loop() {
    // Get the currently display layer to be hidden
    let previousRasterLayer: MGLRasterStyleLayer = ...
    // Get the next layer to be shown
    let nextRasterLayer: MGLRasterStyleLayer = ...

    previousRasterLayer.isVisible = false
    nextRasterLayer.isVisible = true

    // workaround
    previousRasterLayer.rasterOpacity = NSExpression(forConstantValue: 0.01)
    nextRasterLayer.rasterOpacity = NSExpression(forConstantValue: 1)
}

虽然这种方法可行,但它并不是最佳选择,因为Mapbox SDK仅在设置为可见时才开始加载图层png。这将导致图层在加载时变得模糊。 一个小的解决方法是将栅格图层的不透明度设置为0.01,在这种情况下,Mapbox将立即开始全部加载它们。

问题是是否有更好的方法来实现这一目标。

0 个答案:

没有答案