在Openlayers 2
的情况下,我可以获取要素信息,但如何在Openlayers 3
中从矢量图层获取要素信息
下面的代码如何提取特征信息?
var layerWFS = new ol.layer.Vector({
source: new ol.source.Vector({
loader: function(extent) {
$.ajax('http://localhost:8080/geoserver/wfs', {
type: 'GET',
data: {
service: 'WFS',
version: '1.1.0',
request: 'GetFeature',
typename: 'dgm:all_block_boundary_point',
srsname: 'EPSG:3857',
bbox: extent.join(',') + ',EPSG:3857'
}
}).done(function(response) {
layerWFS
.getSource()
.addFeatures(new ol.format.WFS()
.readFeatures(response));
// console.log(response);
});
},
strategy: ol.loadingstrategy.bbox,
projection: 'EPSG:3857'
})
});
map.addLayer(layerWFS);
发布于 2018-06-01 14:15:13
您可以使用feature.getProperties()
获取所有属性,也可以使用feature.get()
获取特定属性的特性信息。
编辑:看起来他想通过选择来检索特征信息。以下是OP的解决方案。
我已经通过使用ol.interaction.Select()
找到了解决方案
var selectSingleClick = new ol.interaction.Select();
map.addInteraction(selectSingleClick);
map.on('singleclick', function(event){
layerWFS.getProperties();
layerWFS.once('precompose',function(event){
var selectedFeatures = selectSingleClick.getFeatures();
readFeature(selectedFeatures);
});
});
function readFeature(features){
var myfeature = features.item(0);
console.log(myfeature.get('block_name'));
console.log(myfeature.get('latitude'));
console.log(myfeature.get('longitude'));
}
https://stackoverflow.com/questions/50598847
复制相似问题