Mar*_*ark 6 javascript openlayers-3
我有这个GeoJSON文件(polygon.geojson)...
{
"type": "Feature",
"geometry": { "type": "Polygon", "coordinates": [ [ [73, 15], [83.0, 15], [83, 5], [73, 5], [73, 15] ] ] },
"properties": { "name": "Foo" }
}
Run Code Online (Sandbox Code Playgroud)
...并将其用作矢量源:
var vectorSource = new ol.source.Vector({
url: 'polygon.geojson',
format: new ol.format.GeoJSON(),
projection : 'EPSG:4326',
});
Run Code Online (Sandbox Code Playgroud)
现在我希望得到以下内容:
var extent = vectorSource.getExtent();
Run Code Online (Sandbox Code Playgroud)
extent但是,它的价值是:
Array [ Infinity, Infinity, -Infinity, -Infinity ]
Run Code Online (Sandbox Code Playgroud)
我正在使用OL 3.9.0,并且正确显示了带有此源的矢量图层.我究竟做错了什么?
我想到了.我需要等到源加载:
vectorSource.once('change',function(e){
if(vectorSource.getState() === 'ready') {
var extent = vectorSource.getExtent();
console.log(extent);
map.getView().fit(extent, map.getSize());
}
});
Run Code Online (Sandbox Code Playgroud)
编辑:如果图层不为空,则缩放可能更安全:
vectorSource.once('change',function(e){
if(vectorSource.getState() === 'ready') {
if(layers[0].getSource().getFeatures().length>0) {
map.getView().fit(vectorSource.getExtent(), map.getSize());
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2715 次 |
| 最近记录: |