mapbox-gl - 在 mapbox-gl-js 中切换图 block url 的推荐方法

标签 mapbox-gl mapbox-gl-js

情况

我们将一个栅格图层渲染到 map 上。该图层的源有一个初始的tile-url。现在我们想要更改源的tile-url 并触发新图 block 的重新加载。例如。我们有不同时间点的图 block ,并且我们希望逐步执行不同的时间步骤。

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

这很好用。但是,当然,使用私有(private)方法是不好的做法;请参阅以下原因:

github 上的当前版本可以做什么(最新提交 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 上调用 reload(),而不是在 RasterTileSource 上。看来我们不必再使用任何私有(private)方法,尽管这看起来仍然像未记录的 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

SourceCacheRasterTileSource 实例作为私有(private) _source 字段。

问题

推荐的方法是什么?这是正在开发的功能吗?有没有解释为什么这还不是或永远不会成为一项功能?

最佳答案

听起来您正在尝试更改栅格源的URL。在 GL JS 中执行此操作的正确方法是删除源,然后添加具有相同 id 和新 url 的新源。

map.addSource('tile-source', {...});
map.addLayer('tile-layer', {source: 'tile-source', ...});

// react to a button click or what ever to trigger tile url change
...
map.removeSource('tile-source');
map.addSource('tile-source', {tiles: ['new-tile-url'], ...});

关于mapbox-gl - 在 mapbox-gl-js 中切换图 block url 的推荐方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38631344/

相关文章:

javascript - 如何在 Mapbox GL JS 中的可点击多边形顶部放置可点击标记

javascript - 考虑到俯仰 Angular 和方位 Angular ,如何获取 mapbox-gl map 视口(viewport)边缘坐标?

mapbox - 从 mapbox geocoder 中检索数据

css - 设置 Mapbox 弹出窗口的指针/指示器的样式

javascript - Mapbox GL JS - 数据驱动样式 - 查找图层中数据的最小值和最大值或范围

android - 如何在 MapBox GL 中旋转 MapView?

javascript - map 框 gl js : use intermediate rendering step as mask for custom layer

mapbox - 使用 mapbox-gl-js 聚类自定义 html 标记

flutter - 有没有办法向 map 的 Mapbox 添加自定义图标?

javascript - MapBox 中的 Source、Layer 和 Tileset 有什么区别?