简体   繁体   English

如何在OpenLayers中获取选定功能的Event或DOM元素

[英]How to get Event or DOM element of selected feature in OpenLayers

I'm implementing an OpenLayers SelectFeature control, and trying to position an JQuery UI dialog widget right on top of the selected feature. 我正在实现OpenLayers SelectFeature控件,并尝试将JQuery UI对话框小部件放置在所选功能的顶部。 To use the JQuery UI Position utility, it requires either a DOM element or an Event. 要使用JQuery UI Position Utility,它需要DOM元素或Event。

The onSelect callback of the SelectFeature control gives me an OpenLayers.Feature.Vector object representing the selected feature. SelectFeature控件的onSelect回调为我提供了代表所选功能的OpenLayers.Feature.Vector对象。 From this, how do I get either the DOM element of the selected feature, or the Event object of the click event? 由此,如何获得所选功能的DOM元素或click事件的Event对象?

  var selectControl = new OpenLayers.Control.SelectFeature(clientsLayer, {
            hover   : false,
            clickout: false,
            multiple: false,
            onSelect: function(feature) {
                // how do I get the DOM element of the feature
                // or alternately, the click event of the selection?
            }
   }); 

You are doing it right. 您做对了。

If you do a console.log(feature) You'll see that it returns an object with CLASS_NAME = "OpenLayers.Feature.Vector" 如果执行 console.log(feature)您将看到它返回的对象为CLASS_NAME =“ OpenLayers.Feature.Vector”

 
 
 
  
  onSelect: function(feature) { console.log(feature); }
 
  

Update: 更新:

I see. 我懂了。 You could add event listeners 您可以添加事件监听器

 var selectControl = new OpenLayers.Control.SelectFeature(clientsLayer, { hover: false, clickout: false, multiple: false, eventListeners: { featurehighlighted: function (event) { console.log(event); console.log(event.feature); } } }); 

Is it something like this you look for ? 您要寻找这样的东西吗?

onSelect: function onFeatureSelect(event) {
              var feature = event.feature;
              if ( feature.layer.name == 'theone') {
              ...
              }
        }

Note I have also posted this answer at How do I get the DOM element from openlayers vector feature 请注意,我也在“ 如何从openlayers矢量功能获取DOM元素”中发布了此答案。

If you want to find the position of the mouse or feature on hover so you can display a custom overlay, create a custom hover control and define the featurehighlighted function as follows: 如果要查找鼠标或要素在悬停时的位置,以便显示自定义叠加层,请创建自定义悬停控件并按如下所示定义功能突出显示的功能:

var featureHoverControl = new OpenLayers.Control.SelectFeature([myLayer], {
    id: 'featureHoverControl',
    hover: true,
    autoActivate: true,
    highlightOnly: true,
    renderIntent: "temporary",
    eventListeners: {
       featurehighlighted: function(e) {
            // either use the mouse's position when the event was triggered
            var mouseXPosition = this.handlers.feature.evt.x;
            var mouseYPosition = this.handlers.feature.evt.y;

            // or retrieve the feature's center point
            var featureCenterLonLat = e.feature.geometry.bounds.getCenterLonLat();
            var featureCenterPoint = map.getPixelFromLonLat(featureCenterLonLat);

            // display your custom popup here
            // e.g. showTooltip(e.feature.attributes.name, featureCenterPoint.x, featureCenterPoint.y)
        }
        ,
        featureunhighlighted: function(e) {
            // hide your custom popup here
            // e.g. hideTooltip()
        }
    }
});
map.addControl(featureHoverControl);

If you require access to the SVG element representing your layer/feature (in the event you are using a third-party library and you don't feel like modifying the source code), use either of the following lines (depending if you require the layer or feature): 如果您需要访问表示您的图层/功能的SVG元素(如果您使用的是第三方库,并且不想修改源代码),请使用以下任意一行(取决于您是否需要使用图层或要素):

var layerElement = map.getLayersByName("My Layer")[0].features.root;
var layerElementId = map.getLayersByName("My Layer")[0].features.root.id;
var featureElementId = map.getLayersByName("My Layer")[0].getFeaturesByAttribute("name","My Feature Name")[0].geometry.components[0].id;

Note that since this only grabs the element's id, you'll still need to use an appropriate method to grab a reference to the element itself. 请注意,由于这仅获取元素的ID,因此您仍然需要使用适当的方法来获取对元素本身的引用。 Something like either of the following: 类似于以下任何一种情况:

var elementReference1 = document.getElementById(featureElementId);
var elementReference2 = jQuery('#'+featureElementId)[0];

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

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