Jam*_*mes 19 javascript geojson
我有一个leaflet.js地图,其上有点和线串,来自外部JSON文件.
如果我添加:map.setView(new L.LatLng(0,0),10);
它将地图置于纬度和经度0,0的中心.如何设置它以使地图中心和缩放适合JSON上的所有点?
Jas*_*son 21
您可以将所有图层添加到具有getBounds
方法的FeatureGroup中.所以你应该能够说出来myMap.fitBounds(myFeatureGroup.getBounds());
L.FeatureGroup的getBounds方法目前至少在主分支(不是最新版本,0.3.1)中可用.
与我类似的情况.我从GeoJson数据中绘制了所有标记.所以我写了这个函数,在按钮点击时反复调用它.试试它是否符合您的要求.
function bestFitZoom()
{
// declaring the group variable
var group = new L.featureGroup;
// map._layers gives all the layers of the map including main container
// so looping in all those layers filtering those having feature
$.each(map._layers, function(ml){
// here we can be more specific to feature for point, line etc.
if(map._layers[].feature)
{
group.addLayer(this)
}
})
map.fitBounds(group.getBounds());
}
Run Code Online (Sandbox Code Playgroud)
编写此函数的最佳用途是,即使地图/标记的状态发生变化,它也会获得标记/图层的最新/当前状态.每当调用此方法时,所有图层都将对适度缩放级别可见.