openlayers简单鼠标悬停在标记上

时间:2011-12-15 16:35:01

标签: openlayers mouseover marker infowindow

这听起来很简单,但我找不到任何新手教程:有人能给我一个简单的例子我如何在OpenLayers中创建(vektor)标记,在mouseover上打开infowindow甚至在mouseout上关闭它?

我发现了部分解释但不是全部......

8 个答案:

答案 0 :(得分:16)

有关如何执行此操作的简单示例,您需要执行以下操作:

创建一个矢量图层以包含您的标记并将其添加到地图中:

this.markerLayer = new OpenLayers.Layer.Vector(
    "My Marker Layer",
    { /* configuration options are set here */ }
);
map.addLayer(this.markerLayer);

创建标记并将其添加到地图中:

var marker = new OpenLayers.Feature.Vector(point, attributes, markerStyle);
this.markerLayer.addFeatures([marker]);

为图层创建一个选择控件,并在用户将鼠标悬停在标记上时注册一个函数来构建弹出窗口:

var selectControl = new OpenLayers.Control.SelectFeature(this.markerLayer, {
    hover: true
});
selectControl.events.register('featurehighlighted', null, onFeatureHighlighted);

构建弹出窗口:

onFeatureHighlighted: function (evt) {
    // Needed only for interaction, not for the display.
    var onPopupClose = function (evt) {
        // 'this' is the popup.
        var feature = this.feature;
        if (feature.layer) {
            selectControl.unselect(feature);
        }  
        this.destroy();
    }

    feature = evt.feature;
    popup = new OpenLayers.Popup.FramedCloud("featurePopup",
            feature.geometry.getBounds().getCenterLonLat(),
            new OpenLayers.Size(100,100),
            "<h2>"+feature.attributes.station_na + "</h2>" +
            "Location: " + feature.attributes.location + '<br/>' +
            "Elevation: " + feature.attributes.elev_,
            null, true, onPopupClose);
    feature.popup = popup;
    popup.feature = feature;
    map.addPopup(popup, true);
}, // ...

答案 1 :(得分:9)

您可以使用标记和弹出

示例:

var popup;
var marker_layer = new OpenLayers.Layer.Markers( "Markers" );
var size = new OpenLayers.Size(32,32);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon_marker = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png',
                                       size, 
                                       offset);
marker = new OpenLayers.Marker(new OpenLayers.LonLat(5.6, 50.6), icon_marker);

//here add mouseover event
marker.events.register('mouseover', marker, function(evt) {
    popup = new OpenLayers.Popup.FramedCloud("Popup",
        new OpenLayers.LonLat(5.6, 50.6),
        null,
        '<div>Hello World! Put your html here</div>',
        null,
        false);
    map.addPopup(popup);
}
//here add mouseout event
marker.events.register('mouseout', marker, function(evt) {popup.hide();});

marker_layer.addMarker(marker);
map.addLayer(marker_layer);

答案 2 :(得分:2)

这对我有用。结束很简单,但需要一段时间:

 var marker = new OpenLayers.Marker(position, icon.clone());           
 boothLayer.addMarker(marker);                                         

 marker.events.register('mouseover', marker, function() {           
   var msg = booth.get('name') +' - ' + booth.get('premises');      
   var popup = new OpenLayers.Popup.FramedCloud("Popup",            
       position, null, '<div>'+msg+'</div>', null, false);          
   map.addPopup(popup);                                             
   marker.events.register('mouseout', marker,                       
     setTimeout( function() { popup.destroy(); }, 4000));           
   });                                                              

请注意鼠标输出事件的4000微秒延迟 - 可能更短,取决于您的使用情况。

答案 3 :(得分:1)

您需要使用selectControl来创建弹出窗口。要使其响应悬停而不是在构造函数中单击设置hover:true。

答案 4 :(得分:1)

我使用一个函数来添加它,这是一个简化版本。请注意,您可以设置详细信息并调用底部的功能。另请注意,您不能在多个图层上拥有多个选择器 - 我认为只添加了最后一个选择器才有效,因此如果您想在一个图层中使用多个要素,则必须进行调整:

function addMarkerLayer(markerInfo){
    var details= markerInfo.details || "<h3>"+markerInfo.title+"</h3>"+
        "[Location] Lat:"+markerInfo.latitude + " Lon:"+markerInfo.longitude;

    var features=[];
    features.push(new OpenLayers.Feature.Vector(
        new OpenLayers.Geometry.Point(markerInfo.longitude, markerInfo.latitude),
        {title: markerInfo.title, details:details, externalGraphic:markerInfo.icon},
        {
            externalGraphic:markerInfo.icon,
            fillColor: markerInfo.markerColor || '#ff0',
            fillOpacity: markerInfo.iconOpacity || 0.8,
            graphicWidth   : markerInfo.iconSize || 24,
            graphicHeight  : markerInfo.iconSize || 24,
            strokeColor: markerInfo.markerStrokeColor || "#ee9900",
            strokeOpacity: 1,
            strokeWidth: 1,
            pointRadius: 7
        }
    ));

    // create the layer with listeners to create and destroy popups
    var vector = new OpenLayers.Layer.Vector(markerInfo.layerName, {
        eventListeners: {
            'featureselected': function(evt) {
                var feature = evt.feature;
                var popup = new OpenLayers.Popup.FramedCloud("popup",
                    OpenLayers.LonLat.fromString(feature.geometry.toShortString()),
                    null,
                    feature.attributes.details,
                    null,
                    true);
                feature.popup = popup;
                map.addPopup(popup);
            },
            'featureunselected': function(evt) {
                var feature = evt.feature;
                map.removePopup(feature.popup);
                feature.popup.destroy();
                feature.popup = null;
            }
        }
    });
    vector.addFeatures(features);

    var selector = new OpenLayers.Control.SelectFeature(vector, {
        hover: true
    });

    map.addLayer(vector);
    map.addControl(selector);
}


var markerInfo={
    title:'Washington DC',
    latitude:38.8,
    longitude:-77,
    icon:"/icons/event.png",
    iconSize:24
};
addMarkerLayer(markerInfo);

答案 5 :(得分:0)

This example from a userlist非常有助于展示如何将hover和click事件分配给矢量图层。

答案 6 :(得分:0)

我用ol 5.2示例“自定义交互”编写了一个有效的示例 openlayers.org/en/latest/examples/select-features.html

因此,您将要素添加到图层并将其添加到地图,然后像这样创建新的交互

const interact = new ol.interaction.Select({condition: ol.events.condition.pointerMove});

它指定它将在悬停时选择一个功能(pointermove) 然后将其添加到地图中,并关联在交互选择要素时(即,当鼠标悬停在某个要素上时)应调用的函数。

map.addInteraction(interact);
interact.on('select', showInfoWindow);

然后您声明显示信息窗口的函数

function showInfoWindow(evt){
    if(evt.selected.length>0){
        const feature = evt.selected[0];
        const id = feature.getId();

        infoWindow.innerHTML = '<p>' + id + '</p>';
        infoWindow.show();//if you have something like that, you could use an openLayers overlay
    }
}

您应注意,该事件将返回(在这种情况下)一个对象,当您将鼠标悬停在该对象中时,可以在该对象中的“ selected”属性中找到所选特征。当您将鼠标悬停在这种情况下时,相同的对象将在“取消选中”属性中可用,直到您选择一个新对象为止。

答案 7 :(得分:-1)

听起来你想看看“OpenLayers.Popup()”

此示例在绘制多边形后显示一个弹出窗口,然后单击它,但您应该能够更改它以响应悬停。 http://openlayers.org/dev/examples/select-feature-openpopup.html