2011-12-21 7 views
4

マップ上にフィーチャーが散在したopenlayersベクトルレイヤーがあります。私は機能をクリックしてメッセージを表示できるようにしたい。OpenLayersベクトルレイヤーフィーチャーハンドラー

各機能にリスナー/ハンドラーを追加する方法があるかどうかはわかりません。

アイデア?

答えて

7

SelectFetureコントロールを追加します。その後

var selectFeature = new OpenLayers.Control.SelectFeature(vector_layer); 
map.addControl(selectFeature); 
selectFeature.activate(); 

をあなたは/ベクトルレイヤー上の選択を解除したイベントを選択するために聞くことができます。多くのベクトルレイヤーがある場合は

vector_layer.events.on({ 
    'featureselected': function(feature) { 
     //display your message here 
    }, 
    'featureunselected': function(feature) { 
     //hide message 
    } 
}); 
0

は「LAYER_NAMEを書くことが必要です。 events.on ... "各層のために?レイヤのリストを作成し、それらのすべてに ".events.on"を割り当てることは可能ですか?

+0

私は自分で試してみませんでしたが、SelectControlを作成すると、explplのようなものではなくベクトルレイヤーの配列を送信できます上記のe。次に、selectControl.onSelect(){};でcatch selectイベントを実行できるようにする必要があります。関数。 – igorti

4

SelectFeatureコントロールとOpenLayers.Popup.FramedCloudのようなOpenLayers.Popupクラスのいずれかの組み合わせを使用する必要があります。ここではそれだけの例です。この例で

http://openlayers.org/dev/examples/select-feature-openpopup.html

、(多角形を完了するために、マップ上でダブルクリックして)ポリゴンを描画する「多角形を描く」オプションを使用してみてください。次に、「クリックしてポリゴンを選択」を使用してポリゴンをクリックすると、枠付きのクラウドポップアップが表示されます。

ページのソースを表示して、これがどのように行われているかを確認できます。ここにコードの関連部分があります。コントロールの参照は、あなたが使用されますされている。ここ

var map = <your OpenLayers.Map object>; 
    var polygonLayer = <your vector layer>; 

    selectControl = new OpenLayers.Control.SelectFeature(polygonLayer, 
      {onSelect: onFeatureSelect, onUnselect: onFeatureUnselect}); 
    map.addControl(selectControl); // not in the example, but do this 

    function onPopupClose(evt) { 
     selectControl.unselect(selectedFeature); 
    } 

    function onFeatureSelect(feature) { 
     var message = "<div style='font-size:.8em'>Feature: " + feature.id +"<br>Area: " + feature.geometry.getArea()+"</div>"; 

     selectedFeature = feature; 
     popup = new OpenLayers.Popup.FramedCloud("chicken", 
      feature.geometry.getBounds().getCenterLonLat(), 
      null, 
      message, 
      null, true, onPopupClose); 
     feature.popup = popup; 
     map.addPopup(popup); 
    } 

    function onFeatureUnselect(feature) { 
     map.removePopup(feature.popup); 
     feature.popup.destroy(); 
     feature.popup = null; 
    } 

+0

この例は1つのレイヤーですが、2つのVectorレイヤーで作業を開始し、その2つ目のコントロールを追加/アクティブ化してホバーを処理する必要があることに気付きました。 (そして、他の作業をするために働くものを非アクティブにする必要があります)。奇妙な。 –