【问题标题】:Using select and hover listener in openlayers 2 - hover change style在 openlayers 2 中使用选择和悬停侦听器 - 悬停更改样式
【发布时间】:2016-05-25 12:36:09
【问题描述】:

我在鼠标悬停时的样式功能有一些问题。我能够得到事件,但改变风格根本行不通。对于选择它确实如此,但对于悬停/鼠标悬停它根本没有。谁能帮我解决这个问题。这是我的代码:

var map, vectorLayer, wmsLayer, selectControl;

window.onload = function() {

    map = new OpenLayers.Map('map');

    wmsLayer = new OpenLayers.Layer.WMS("OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0?", {
            layers: 'basic'
        }, {
            'attribution': 'Provided by OSGeo'
        }
    );

    vectorLayer = new OpenLayers.Layer.Vector("My Layer Name", {
        styleMap: new OpenLayers.StyleMap({
            "default": new OpenLayers.Style({
                strokeColor: "#ff0000",
                strokeOpacity: .7,
                strokeWidth: 1,
                fillColor: "#ff0000",
                fillOpacity: 0,

                'pointRadius': 30
            }),
            "temporary": new OpenLayers.Style({
                strokeColor: "#ffff33",
                strokeOpacity: .9,
                strokeWidth: 2,
                fillColor: "#ffff33",
                fillOpacity: .3,
                cursor: "pointer",
                'pointRadius': 20
            }),
            "select": new OpenLayers.Style({
                strokeColor: "#0033ff",
                strokeOpacity: .7,
                strokeWidth: 2,
                fillColor: "#0033ff",
                fillOpacity: 0,
                graphicZIndex: 2,

                'pointRadius': 10
            })
        })
    });


    map.addLayers([wmsLayer, vectorLayer]);
    map.zoomToMaxExtent();

    vectorLayer.addFeatures([
        new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Point(10, 10)
        )
    ]);


    var selectControlClicks = new OpenLayers.Control.SelectFeature(vectorLayer, {
        onSelect: function(feature) {
            console.log('select: number of selected features: ' + vectorLayer.selectedFeatures.length);
        },
        onUnselect: function(feature) {
            console.log('unselect: number of selected features: ' + vectorLayer.selectedFeatures.length);
        }
    });
    var selectControlHover = new OpenLayers.Control.SelectFeature(vectorLayer, {
        hover: true,
        highlightOnly: true,
        renderIntent: 'temporary',
        overFeature: function(feature) {
            console.log('hover: number of selected features: ' + vectorLayer.selectedFeatures.length);
        },
        outFeature: function(feature) {
            console.log('hover out: number of selected features: ' + vectorLayer.selectedFeatures.length);
        },
    });
    map.addControl(selectControlHover);
    selectControlHover.activate();
    map.addControl(selectControlClicks);
    selectControlClicks.activate();

}

http://jsfiddle.net/eW8DV/80/

也许我的洞方法是错误的,也许我应该只使用一个 SelectControl ?

干杯

【问题讨论】:

    标签: javascript select hover openlayers mouseover


    【解决方案1】:

    我认为你的问题在于 overFeature 和 outFeature 的声明。

    实际上,虽然 onSelect 和 onUnselect 是模板方法,旨在被覆盖,但 overFeature 和 outFeature 不是。 覆盖这些方法会导致覆盖默认行为( layer.drawFeature(feature, style); )。

    无论如何,我建议您改用事件。试试

    selectControlHover.events.register('featurehighlighted', null, function(e) {
    console.log('feature selected ', e.feature);
    });
    

    另外,我很确定您可以使用一个控件而不是两个控件,但不知道您要做什么,我不能建议您使用其他方法。

    【讨论】:

    • 你是对的。问题在于悬停侦听器的声明。使用您的方法,它可以完美运行!谢谢!我正在考虑只使用一个选择控制器,但如果我想要悬停和选择,这似乎并不容易。最后我想要实现的是: - 有可能点击功能 - 有可能将鼠标悬停在功能上 - 有可能以编程方式选择一个功能,老实说,只是在地图上选择它而不调用 featuresselected 侦听器(仅在选择以编程方式完成)。这就是洞的情况。再次感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 2011-11-18
    • 1970-01-01
    • 2018-09-04
    • 2023-03-15
    • 2013-06-17
    相关资源
    最近更新 更多