[英]OpenLayers 3 get vector feature/attribute without adding layer to map
I need to access a vector layer's attributions, as it contains information I will use logically within my OL3 implementation. 我需要访问矢量层的属性,因为它包含在OL3实现中将在逻辑上使用的信息。
I can do this as follows: 我可以这样做,如下所示:
//Adding local layer
var layer_to_return = new ol.layer.Vector({
source: new ol.source.Vector({
url: "/positional_data/Flight_Test_Position_Data_GEOJSON_point.geojson",
format: new ol.format.GeoJSON(),
style:Custom_Style,
visible:false
})
});
map.addLayer(layer_to_return);
Notice that I have set visible:false. 注意,我设置了visible:false。 It appears that I need to add the layer to the map in order to access the attributes with the following:
看来我需要将图层添加到地图上,以便通过以下方式访问属性:
layer_to_return.getSource().once('change',function(e){
if(layer_to_return.getSource().getState() === 'ready') {
layer_to_return.getSource().forEachFeature(function(feature){
var time = feature.get('time(millisecond)');
console.log(time);
}
);
}
});
If I don't include the map.addLayer(layer_to_return) statement then the above doesn't work, it just doesn't run the whole change event. 如果我不包含map.addLayer(layer_to_return)语句,则以上内容将不起作用,它不会运行整个更改事件。
If I take the change event handler away, the time variable returns blank, possibly because the layer hasn't loaded yet. 如果我放弃更改事件处理程序,则时间变量将返回空白,这可能是因为尚未加载图层。
Is there a way to get access to the layer attributes without adding the layer to the map? 有没有办法在不将图层添加到地图的情况下访问图层属性?
just make a post-get request to get your json file and then use ol.format.GeoJSON
class to parse the features. 只需发出获取请求即可获取您的json文件,然后使用
ol.format.GeoJSON
类来解析功能。 Something like that should do your job. 这样的事情应该可以做。
$.ajax('/positional_data/Flight_Test_Position_Data_GEOJSON_point.geojson', {
type: 'GET'
}).done(function (geojson) {
//HERE IS YOUR KEY CLASS
var features = new ol.format.GeoJSON().readFeatures(geojson);
//NOW YOU CAN ITERATE THROUGH YOUR FEATURES AND GET ATTR
//LIKE
for (var i=0;i<features.length;i++){
var attr = features[i].get('attr');
}
}).fail(function (jqXHR, textStatus) {
alert('geojson fail to load');
});)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.