【发布时间】:2016-06-10 23:11:38
【问题描述】:
我正在像这样设置 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());
Mapbox GL JS 的fitBounds documentation 表示它想要[[minLng, minLat], [maxLng, maxLat]] 格式的边界。
有没有办法确定这个 GeoJSON 层的混合/最大纬度和经度值?
【问题讨论】:
标签: mapbox mapbox-gl-js