简体   繁体   English

OpenLayers矢量图层特征处理程序

[英]OpenLayers Vector Layer Feature Handler

I would have an openlayers vector layer with features scattered all over the map. 我会有一个openlayers矢量图层,其中的功能散布在整个地图上。 I want to be able to click on a feature and have a message display. 我希望能够点击某个功能并显示一条消息。

I'm not sure if there is a way to add a listener/handler to each feature. 我不确定是否有办法为每个功能添加一个监听器/处理程序。

Any ideas? 有任何想法吗?

Add SelectFeture control: 添加SelectFeture控件:

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

After that you can listen to select/unselect events on vector layer: 之后,您可以在矢量图层上收听选择/取消选择事件:

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

You need to use a combination of the SelectFeature control and one of the OpenLayers.Popup classes such as OpenLayers.Popup.FramedCloud . 您需要使用SelectFeature控件和OpenLayers.Popup类之一的组合,例如OpenLayers.Popup.FramedCloud Here is an example of just that: 这是一个例子:

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

In that example, try using the "draw polygon" option to draw a polygon (double-click on the map to complete the polygon). 在该示例中,尝试使用“绘制多边形”选项绘制多边形(双击地图以完成多边形)。 Then use "select polygon on click" and click on the polygon, and you will get a framed cloud popup. 然后使用“单击选择多边形”并单击多边形,您将获得一个框架云弹出窗口。

You can view the source for the page to see how it this is done. 您可以查看页面的来源以了解它是如何完成的。 Here are the relevant parts of the code. 以下是代码的相关部分。 You can, of course, change the message to whatever you want to display in the framed cloud: 当然,您可以将message更改为要在框架云中显示的内容:

    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;
    }

Here are the references for the controls you will be using: 以下是您将使用的控件的参考:

If there are many vector layers is it necessary to write "layer_name.events.on ..." for each layer? 如果有很多矢量图层,是否需要为每一层写“layer_name.events.on ...”? Is it possible to make a list of layers and assign ".events.on" to all of them? 是否可以制作图层列表并为所有图层分配“.events.on”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM