OpenLayers中的一个功能的Clickhandler

时间:2013-02-01 22:13:39

标签: event-handling click openlayers

我正在尝试在openlayers中弹出一个点击事件。现在我正在使用功能中的硬编码onclick:

    var vector = new OpenLayers.Layer.Vector("Points",{
            eventListeners:{
                'featureselected':function(evt){
                    var feature = evt.feature;
                    var popup = new OpenLayers.Popup.Anchored("popup",
                        OpenLayers.LonLat.fromString(feature.geometry.toShortString()),
                        new OpenLayers.Size(275,71),
                        '<div id="pincontent" onclick="pindetails()"><h3>' + feature.attributes.title +'</h3><div style="display: none;" id="pindescription">'+  feature.attributes.content +'</div></div>',
                        null,
                        false
                    );
                    popup.imageSrc = 'img/popup.png';
                    popup.autoSize = false;
                    popup.backgroundColor = 'transparent';
                    var offset = {'size':new OpenLayers.Size(0,0),'offset':new OpenLayers.Pixel(-74,-10)};
                    popup.anchor = offset;
                    popup.panMapIfOutOfView = true;
                    popup.imageSize = new OpenLayers.Size(275,71);
                    popup.relativePosition = "br";
                    popup.calculateRelativePosition = function () {
                         return 'tr';
                    };
                    feature.popup = popup;
                    map.addPopup(popup);
     //adding event listener
                    map.events.register('mousedown', popup, function(evt){alert('help')}, false);
                },
                'featureunselected':function(evt){
                    var feature = evt.feature;
                    map.removePopup(feature.popup);
                    feature.popup.destroy();
                    feature.popup = null;
                }

            }
        });

但主要的OpenLayers div正在拦截点击,所以我必须点击两次..我不确定是否有办法禁用此功能。我查看了openLayers文档,但我不确定如何使用他们的API为单击某个功能添加事件监听器。

2 个答案:

答案 0 :(得分:2)

也许您应该在onclick函数调用后添加return false;以停止事件传播(这是默认值)。 另请看一下注册方法:

this.map.events.register('click', this.map, function handleMapClick(e) { ... }

http://dev.openlayers.org/releases/OpenLayers-2.6/doc/apidocs/files/OpenLayers/Events-js.html#OpenLayers.Events.register

答案 1 :(得分:0)

似乎evt.stopPropagation();不起作用。但是在return true;事件处理程序中使用singleclick可以阻止Event-Propagation向下渗透到层叠的层。