I've just started having a look at OpenLayers 3 and am attempting to zoom to a single feature whose extent is to be a geoJSON object sent by the server (which will change at each refresh, so I can't hard code the zoom/central point). I would like this to happen preferably as soon as the page loads, but I'm struggling to get it to work.
在我尝试从 geoJSON 对象读取几何图形的行中,我不断收到一条错误消息,提示“未捕获的 TypeError:未定义不是函数”,但我不确定如何修复它。geoJSON 字符串看起来不错(我也尝试在将其传递给 readGeometry 之前对其进行解析,但结果相同)。
如果有比我现在更容易/更快的方法来做到这一点,我也很想听听!任何帮助是极大的赞赏。
var feature = new ol.Feature({
});
var view = new ol.View({
center: [0, 0],
zoom: 1
});
var client = new XMLHttpRequest();
client.open('GET', 'http://localhost:3000/extent');
client.send();
client.onreadystatechange=function(){
if (client.readyState==4 && client.status==200){
var geomstring = client.responseText;
console.log(geomstring)
var geojson = new ol.format.GeoJSON();
var geom = geojson.readGeometry(geomstring);
var size = (map.getSize());
feature.setGeometry(geom);
view.fitGeometry(
geom,
size,
{
padding: [170, 50, 30, 150],
constrainResolution: false
});
}
}
var map = new ol.Map({
layers: [raster, vector, feature],
target: 'map',
});