2016-11-12 9 views
2

私はOpenlayers 3を使用しており、選択したオブジェクトに含まれる情報からグラフをリアルタイムで作成したいと考えています。私はプロパティにアクセスする必要がありますが、get( 'myfield')は動作しません。私の特徴はGeoJSONベクトルレイヤーにあります。選択肢から機能情報を取得するol3

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で多くの経験がありません。

答えて

0

私の問題が見つかりました。正しい構文があるべき

var myfeature = features[0]; 

によって機能にアクセスしようとすると:

var myfeature = features.item(0); 

しかし、前の例の機能に[0]取り組んできました。通常のJavaScriptのArrayオブジェクトの上に拡張したものですuはOpenLayersをの文書を見れば3つの http://openlayers.org/en/latest/apidoc/ol.interaction.Select.html#getFeatures selectSingleClick.getFeatures() 戻りol.Collection()オブジェクトが

1

...これがそうである理由を理解することに熱心になります。 ol.Collection()の詳細については、このリンクをご覧ください。

http://openlayers.org/en/latest/apidoc/ol.Collection.html
var myfeature = features[0]; 

配列からオブジェクトを取得するための通常の方法です。

関連する問題