我正在设置一个 Mapbox GL JS 地图,如下所示:
mapboxgl.accessToken = 'pk.my_token';
var cityBoundaries = new mapboxgl.GeoJSONSource({ data: 'http://domain.com/city_name.geojson' } );
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v8',
center: [cityLongitude,cityLatitude],
zoom: 13
});
然后我将 GeoJSON 数据加载到地图上,如下所示:
map.on('style.load', function(){
map.addSource('city', cityBoundaries);
map.addLayer({
'id': 'city',
'type': 'line',
'source': 'city',
'paint': {
'line-color': 'blue',
'line-width': 3
}
});
});
此时,我有一个以我在中指定的位置为中心的地图new mapboxgl.Map
,缩放级别为 13。因此,地图上仅可见一部分 GeoJSON 数据。我想重新居中并重新缩放地图,以便整个 GeoJSON 数据可见。
在 Mapbox JS 中,我将通过将 GeoJSON 数据加载到featureLayer
然后将地图拟合到其边界:
map.fitBounds(featureLayer.getBounds());
The fitBounds 文档 https://www.mapbox.com/mapbox-gl-js/api/#Map.fitBounds对于 Mapbox GL JS 表示它想要以下格式的边界[[minLng, minLat], [maxLng, maxLat]]
.
有没有办法确定此 GeoJSON 层的混合/最大纬度和经度值?