情境
我们在地图上呈现一个光栅(raster)图层。该图层的源具有初始瓦片URL。现在我们想要更改源的瓷砖URL并触发新瓷砖的重新加载。例如,我们有不同时间点的瓷砖,我们想要浏览不同的时间步骤。
在mapbox-gl@0.21.0
中可以做什么
map.addSource('tile-source', {...});
map.addLayer('tile-layer', {source: 'tile-source', ...});
// react to a button click or what ever to trigger tile url change
...
const source = map.getSource('tile-source');
source.tiles = ['new-tile-url'];
source._pyramid.reload();
这个可以正常工作。但是,当然使用私有方法是不好的做法。请看下面的原因:
当前版本(最新提交b155118,2016-07-28)可以做什么。
// init map, add layer, add source, like above
const source = map.getSource('tile-source');
source.tiles = ['new-tile-url'];
map.styles.sources['tile-source'].reload();
这样做是因为之前的
TilePyramid
已被重构为一个SourceCache
。在这里,我们对SourceCache
而不是RasterTileSource
调用reload()
。似乎我们不再需要使用任何私有方法,但仍然看起来像是未记录的API,可能会在未来版本中发生变化。此外,在调用
reload()
时似乎存在内存泄漏问题:
https://github.com/mapbox/mapbox-gl-js/issues/2266
此外,调用reload()
时缓存将被清除。目前看来这不是一个问题。// This yields a `RasterTileSource` instance
map.getSource('tile-source');
// This yields a `SourceCache` instance
map.styles.sources['tile-source'];
// What's really confusing too, at least namingwise
map.getStyle(); // <-- Yields the maps (visual) style
SourceCache
有一个私有的 _source
字段,其实例是 RasterTileSource
。
问题
做类似这样的事情推荐使用什么方法?这是正在开发的功能吗?有没有解释为什么这不是一个功能(但可能会成为一个功能)?