我正在使用 Openlayers 3,希望根据选定对象上包含的信息实时创建图表。我需要访问属性,但 get('myfield') 不起作用。我的功能位于 GeoJSON 矢量图层中。
var selectSingleClick = new ol.interaction.Select();
map.addInteraction(selectSingleClick);
map.on('singleclick', function(event){
mylayer.once('precompose',function(event){
var selectedFeatures = selectSingleClick.getFeatures();
readFeature(selectedFeatures);
});
});
function readFeature(features){
consoleText = document.getElementById('console');
// When selected, getLength() returns 1, so selection is working.
// consoleText.innerHTML = features.getLength();
var myfeature = features[0];
consoleText.innerHTML += myfeature.get('objectId');
}
任何人都可以帮助我了解出了什么问题吗?我对 Javascript 没有太多经验。
最佳答案
我发现了我的问题。尝试通过以下方式访问功能
var myfeature = features[0];
正确的语法应该是:
var myfeature = features.item(0);
但在前面的示例中,features[0] 已经起作用了。很想了解为什么会这样......
关于openlayers-3 - 从选择ol3中获取特征信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40558197/