OpenLayers 3 获取矢量 feature/attribute 而不向地图添加图层
OpenLayers 3 get vector feature/attribute without adding layer to map
我需要访问矢量图层的属性,因为它包含我将在我的 OL3 实现中合理使用的信息。
我可以这样做:
//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);
注意我设置了visible:false。看来我需要将图层添加到地图才能访问具有以下属性的属性:
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);
}
);
}
});
如果我不包含 map.addLayer(layer_to_return) 语句,那么上面的语句不起作用,它只是行不通 运行整个变化事件。
如果我拿走 change 事件处理程序,时间变量 returns 空白,可能是因为图层尚未加载。
有没有办法在不将图层添加到地图的情况下访问图层属性?
只需发出 post-get 请求来获取您的 json 文件,然后使用 ol.format.GeoJSON
class 解析功能。类似的东西应该可以完成你的工作。
$.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');
});)
我需要访问矢量图层的属性,因为它包含我将在我的 OL3 实现中合理使用的信息。
我可以这样做:
//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);
注意我设置了visible:false。看来我需要将图层添加到地图才能访问具有以下属性的属性:
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);
}
);
}
});
如果我不包含 map.addLayer(layer_to_return) 语句,那么上面的语句不起作用,它只是行不通 运行整个变化事件。
如果我拿走 change 事件处理程序,时间变量 returns 空白,可能是因为图层尚未加载。
有没有办法在不将图层添加到地图的情况下访问图层属性?
只需发出 post-get 请求来获取您的 json 文件,然后使用 ol.format.GeoJSON
class 解析功能。类似的东西应该可以完成你的工作。
$.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');
});)