openlayers-3 - 从选择ol3中获取特征信息

标签 openlayers-3

我正在使用 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/

相关文章:

javascript - GeoJSON 要素坐标未显示在 OpenLayers map 上

openlayers - 以编程方式触发开放层 4 中的选择事件

javascript - 将 geojson 层添加到 openlayers 3

gis - 如何获得对 openlayers 3 中修改功能的引用

javascript - openlayer 3.5 从点获取特征ID

javascript - 无法使用 OpenLayers 3 站点中的 WFS 示例作为基础加载另一个 WFS

javascript - Openlayers3中可以用ctrl拖动 map 吗?

javascript - GeoServer - DWITHIN 未正确过滤点

javascript - 将 map 外部的内容拖放到要素(openlayers)上?

openlayers-3 - 开放层 3.6 : Getting the center of the current map view