如何在不删除选择控件的情况下突出显示鼠标悬停

时间:2013-07-11 08:38:38

标签: javascript select openlayers mouseover highlight

在OpenLayers中,我试图在光标移过它们时突出显示矢量图层的特征。我可以做这个工作。

我也试图让这些功能可以选择,并在点击时弹出一个框。 这也有效。

我不能做的工作同时也是他们两个。 hoverselect似乎超出了弹出选择。我的代码基于http://dev.openlayers.org/releases/OpenLayers-2.11/examples/highlight-feature.html,但有一些差异。有谁知道为什么悬停选择和选择不能共存?

select = new OpenLayers.Control.SelectFeature(vector_Layer, {clickout: true}); 
        vector_Layer.events.on({
            "featureselected": onFeatureSelect,
            "featureunselected": onFeatureUnselect
        });
        map.addControl(select);
        select.activate();

        select.handlers['feature'].stopDown = false; 
        select.handlers['feature'].stopUp = false;

//REMOVED AS OVERRODE SELECT selectHover = new OpenLayers.Control.SelectFeature(vector_Layer, {hover: true});

function onPopupClose(event) {
    select.unselectAll();
}

function onFeatureSelect(event) {
        var feature = event.feature;
        var content = "<p>"+feature.attributes.SHEET.value + "</p>" + feature.attributes.description;
        if (content.search("<script") != -1) {
            content = "Content contained Javascript! Escaped content below.<br>" + content.replace(/</g, "&lt;");
        }
        popup = new OpenLayers.Popup.FramedCloud("chicken", 
                                 feature.geometry.getBounds().getCenterLonLat(),
                                 new OpenLayers.Size(100,100),
                                 content,
                                 null, true, onPopupClose);
        feature.popup = popup;
        map.addPopup(popup);
}  

function onFeatureUnselect(event) {
        var feature = event.feature;
        if(feature.popup) {
            map.removePopup(feature.popup);
            feature.popup.destroy();
            delete feature.popup;
        }
}

var info = function(evt) {
            OpenLayers.Console.log(evt.type, evt.feature.id);
        };

//REMOVED OF OVERODE SELECT var highlight = new OpenLayers.Control.SelectFeature(vector_Layer, {
//                hover: true,
//                highlightOnly: true,
//                renderIntent: "temporary",
//                eventListeners: {
//                    beforefeaturehighlighted: info ,
//                    featurehighlighted: info ,
//                    featureunhighlighted: info 
//                }
//            });
//map.addControl(highlight);
//highlight.activate();

var vector_style_d = new OpenLayers.Style({
    'fillColor': '#669933',
    'fillOpacity': .5,
    'strokeColor': '#aaee77',
    'strokeWidth': 3,
    'cursor': 'pointer'
})

var vector_style_s = new OpenLayers.Style({
    'fillColor': '#FE2E2E',
    'fillOpacity': .8,
    'strokeColor': '#aaee47',
    'strokeWidth': 4,
    'cursor': 'pointer'
})

var vector_style_t = new OpenLayers.Style({
    'fillColor': '#FE2E2E',
    'fillOpacity': .8,
    'strokeColor': '#aaee47',
    'strokeWidth': 3,
    'cursor': 'pointer'
})

var vector_style_map = new OpenLayers.StyleMap({
    'default': vector_style_d,
    'select': vector_style_s,
    'temporary': vector_style_t,
});

vector_Layer.styleMap = vector_style_map;

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但正在查看OL页面提供的示例(http://openlayers.org/dev/examples/highlight-feature.html) 对我来说诀窍是改变调用两个控件的构造函数的顺序(它应首先悬停,然后选择)。

干杯