我已经浏览了Mapbox的所有fitbounds教程,仍然无法弄清楚如何将我的地图重新聚焦在给定的光栅层上。我有一个菜单,切换一些较旧的地图,并希望在每个回合的地图。
到目前为止我的情况是这样的。mapnumber是我的光栅地图的字符串。
var coordinates = map.getBounds(mapnumber);
var bounds = new mapboxgl.LngLatBounds(coordinates[0], coordinates[0]);
map.fitBounds({bounds});边界在那里,我只是不能对它们使用限界。这就是错误。
lng_lat.js:97 Uncaught Error: `LngLatLike` argument must be specified
as a LngLat instance, an object {lng: <lng>, lat: <lat>}, or an array
of [<lng>, <lat>]发布于 2017-09-01 18:15:19
未知错误:必须将
LngLatLike参数指定为LngLat实例
看起来您使用的mapboxgl.LngLatBounds不正确。您需要传入两个LngLat实例,而不是两个坐标。API文档有一个示例,为包围框的西南角和东北角创建一个点:
var sw = new mapboxgl.LngLat(-73.9876, 40.7661);
var ne = new mapboxgl.LngLat(-73.9397, 40.8002);
var llb = new mapboxgl.LngLatBounds(sw, ne);然后,您可以在边界框中使用map.fitBounds()。
map.fitBounds(llb);更新:回答后续问题
如何从栅格中提取这两组(sw和ne)?
您可以使用方法。传入光栅层的源ID,这将返回具有bounds属性的对象。使用此界限属性传递到map.fitBounds()。
var bounds = map.getSource('mapbox://username.source-id').bounds;
map.fitBounds(bounds);下面是一个有用的示例:https://codepen.io/mapsam/pen/RZvyBQ
https://stackoverflow.com/questions/45997911
复制相似问题