Javascript 地图框适合国家/地区
我只想让我的地图适合我的国家。我从中看到了一些例子,它适用于整个肯尼亚。这是一个简单的代码,但我不知道为什么这个函数需要两个lat和long。我只是在谷歌上搜索什么是Keyna lat和long?它是1.2667°s,36.8000°E。为什么这与谷歌的结果不同Javascript 地图框适合国家/地区,javascript,mapbox,Javascript,Mapbox,我只想让我的地图适合我的国家。我从中看到了一些例子,它适用于整个肯尼亚。这是一个简单的代码,但我不知道为什么这个函数需要两个lat和long。我只是在谷歌上搜索什么是Keyna lat和long?它是1.2667°s,36.8000°E。为什么这与谷歌的结果不同 function fit() { map.fitBounds([[ 32.958984, -5.353521 ], [ 43.50585, 5.615985
function fit() {
map.fitBounds([[
32.958984,
-5.353521
], [
43.50585,
5.615985
]]);
}
如何适合我的特定领域,就像这样 .fitBounds接受两个latlng参数,一个用于地图视图的左上角,另一个用于右下角 如果您想将地图放在肯尼亚的中心,您可以使用:
map.flyTo({center: [Lat, Lng]})
如果我搜索肯尼亚的边界框,我会发现以下内容: 使用这些坐标,看起来还可以:
map.fitBounds([
[-4.71712, 33.90884], // Northeast
[4.62933, 41.899059] // Southwest
]);
使用Plunker传单的例子:我也在寻找这个问题的答案。例如,传单有一个层的属性来获取其边界(例如,
map.fitBounds(layer.getBounds());
)。Mapbox GL没有类似的东西。至少我不知道。要处理此问题,您可以访问当前所选要素的第一个和最后一个坐标:map.fitBounds([feature.geometry.coordinates[0],feature.geometry.coordinates[feature.geometry.coordinates.length-1]
下面是一段完整的代码,以防您想要一个带有缩放按钮的弹出窗口:
map.on('click', function (e) {
map.featuresAt(e.point, {layer: 'route-lines', radius: 10, includeGeometry: true}, function (err, features) {
if (err || !features.length)
return;
var feature=features[0];
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML(popupTemplate)
.addTo(map);
var buttonZoomFeature = document.getElementById('button-zoom');
buttonZoomFeature.addEventListener('click', function (e) {
map.fitBounds([feature.geometry.coordinates[0], feature.geometry.coordinates[feature.geometry.coordinates.length-1]]);
});
});
});
var popupTemplate = '<div id="popup-div">\
<button id="button-zoom" class="button-zoom" type="button">Zoom to</button>\
</br>\
</div>';
map.on('click',函数(e){
地图特征卫星(e.point,{图层:'路线',半径:10,包括几何:真},功能(err,特征){
if(err | |!features.length)
返回;
变量特征=特征[0];
新建mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML(popupTemplate)
.addTo(地图);
var buttonZoomFeature=document.getElementById('button-zoom');
buttonZoomFeature.addEventListener('click',函数(e){
fitBounds([feature.geometry.coordinates[0],feature.geometry.coordinates[feature.geometry.coordinates.length-1]]);
});
});
});
var popupTemplate='1〕\
缩放到\
\
';
注:当图层为多条线(例如道路)时,此方法效果良好。如果要将边界拟合到一个国家(即多边形),可以尝试将第一个经度点作为具有最小经度的点,将第二个经度点作为具有最大经度的点。或者类似的。试着使用这种方法。我相信它会成功的 如何知道我所在区域的左下角和右下角?你必须弄清楚。。。。使用谷歌地图,点击地图获取特定点的坐标。这里有很多边界数据。最适合我的是磨合文字对象,它需要两个lat-long,一个用于左上角,一个用于右下角。