如何获取GeoJSON向量源的范围? [英] How to get the extent of a GeoJSON vector source?
本文介绍了如何获取GeoJSON向量源的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个GeoJSON文件(polygon.geojson)...
I have this GeoJSON file (polygon.geojson)...
{
"type": "Feature",
"geometry": { "type": "Polygon", "coordinates": [ [ [73, 15], [83.0, 15], [83, 5], [73, 5], [73, 15] ] ] },
"properties": { "name": "Foo" }
}
...并将其用作矢量源:
...and use it as vector source:
var vectorSource = new ol.source.Vector({
url: 'polygon.geojson',
format: new ol.format.GeoJSON(),
projection : 'EPSG:4326',
});
现在我想了解范围:
var extent = vectorSource.getExtent();
extent
的值是:
Array [ Infinity, Infinity, -Infinity, -Infinity ]
我使用的是OL 3.9.0,带有此源的矢量层显示正确.我在做什么错了?
I'm using OL 3.9.0 and the vector layer with this source is displayed properly. What am I doing wrong?
推荐答案
我知道了.我需要等到源被加载:
I figured it out. I need to wait until the source is loaded:
vectorSource.once('change',function(e){
if(vectorSource.getState() === 'ready') {
var extent = vectorSource.getExtent();
console.log(extent);
map.getView().fit(extent, map.getSize());
}
});
编辑:仅当图层不为空时,缩放才可能更安全:
It might be safer to zoom only if the layer isn't empty:
vectorSource.once('change',function(e){
if(vectorSource.getState() === 'ready') {
if(layers[0].getSource().getFeatures().length>0) {
map.getView().fit(vectorSource.getExtent(), map.getSize());
}
}
});
这篇关于如何获取GeoJSON向量源的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文