2016-05-22 5 views
0

マップには、ポイントとポリゴンの2種類があります。各タイプにはid、doorNo、nameなどの独自のプロパティがあります。Open Layers 3で選択したフィーチャのプロパティを動的に取得する方法は?

var features = new ol.format.GeoJSON().readFeatures(geojsonObject, { 
    featureProjection: 'EPSG:3857' 
}); 

インデックスでフィーチャを呼び出すと、get( 'property_name')関数を使用してプロパティを取得できます。例えば;

features[0].getGeometry().getType() == "Point" // check this feature is Point or not 
$doorNo = features[$index].get("doorNo");//this is also works 

しかし同時に私はマップ上にselectInteractionを持っています。そして、私が機能を選択するとき、私はこれらのすべてのプロパティをボタンオンイベントで取得したいと思います。そうするために、私はこれを書いています。

$('#btnSelected').on('click', function() { 
     if (selectInteraction) { 
      // use the features Collection to detect when a feature is selected, 
      // the collection will emit the add event 
      var selectedFeatures = selectInteraction.getFeatures(); 
      console.log("Length: " + selectedFeatures.getLength());// this is works 
      console.log("Coordinates: " + selectedFeatures[0].getGeometry().getCoordinates());//THIS GAVES ME ERROR 
     }else 
      console.log('there is no selected feature.'); 
    }); 

ので、クリックイベントに私はすべての機能プロパティをコンソールに書きたいけどselectedFeatures.lengthは私がすべてで任意のプロパティを取得することはできません正しい数をgaves場合でも。 どこが間違っていますか?

enter image description here

注:マップでは、青い点が選択されます。最初の2つのキャッチされていないエラーは、この質問とは関係ありません。

答えて

1

getFeaturesメソッドは、配列ではなくol.Collectionを返します。

ol.Collectionでは、JSの括弧表記を使用して項目を取得することはできません。 selectedFeatures[0]の代わりにselectedFeatures.item(0)の代わりに。

+0

あなたの答えをありがとう、それは今働きます。 –

+1

ol.CollectionのgetArray()メソッドを使用して、下にある配列を取得することもできます:var selectedFeatures = selectInteraction.getFeatures()。getArray(); –

関連する問題