【问题标题】:Getting feature information from selection ol3从选择 ol3 中获取特征信息
【发布时间】:2016-12-01 20:44:30
【问题描述】:

我正在使用 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 没有太多经验。

【问题讨论】:

    标签: openlayers-3


    【解决方案1】:

    我发现了我的问题。尝试通过

    访问功能
    var myfeature = features[0];
    

    正确的语法应该是:

    var myfeature = features.item(0);
    

    但在前面的示例中, features[0] 已经起作用。很想知道为什么会这样......

    【讨论】:

      【解决方案2】:

      如果你查看 openlayers 3 的文档 http://openlayers.org/en/latest/apidoc/ol.interaction.Select.html#getFeatures selectSingleClick.getFeatures() 返回 ol.Collection() 对象,它是对普通 Javascript 数组对象的增强。 有关 ol.Collection() 的更多信息,请查看此链接。 http://openlayers.org/en/latest/apidoc/ol.Collection.html

      var myfeature = features[0];
      

      是从数组中获取对象的正常方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-03
        • 2021-08-18
        • 2011-03-10
        • 2015-06-03
        • 2011-01-10
        相关资源
        最近更新 更多