以下片段的灵感来自mapbox-gl 上的zoom-to-linestring
示例
正如@Aravind 上面提到的,我们使用map.fitBounds
,但是,我们必须确定边界,我们必须减少特征数组以确定 LngLatBounds。
只要确保你加载了特征数据。
例子
let features = [feature, feature] // ... array of features
// first coordinate in features
let co = features[0].geometry.coordinates;
// we want to determine the bounds for the features data
let bounds = features.reduce((bounds, feature) => {
return bounds.extend(feature.geometry.coordinates);
}, new mapboxgl.LngLatBounds(co[0].lng, co[0].lat));
// set bounds according to features
map.fitBounds(bounds, {
padding: 50,
maxZoom: 14.15,
duration: 2000
});