openlayers简单的鼠标悬停在标记上
Posted
技术标签:
【中文标题】openlayers简单的鼠标悬停在标记上【英文标题】:openlayers simple mouseover on marker 【发布时间】:2012-01-21 08:19:42 【问题描述】:听起来很简单,但我找不到任何新手教程:谁能给我一个简单的例子,我如何在 OpenLayers 中创建(矢量)标记,在鼠标悬停时打开信息窗口,甚至在鼠标移出时关闭它?
我找到了部分解释,但不是全部....
【问题讨论】:
【参考方案1】:您可以使用标记和弹出窗口
示例:
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】:我用 ol 5.2 示例“自定义交互”编写了一个工作示例 openlayers.org/en/latest/examples/select-features.html
因此,您将要素添加到图层并将其添加到地图中,然后像这样创建新的交互
const interact = new ol.interaction.Select(condition: ol.events.condition.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
您应该注意,该事件将返回(在这种情况下)一个对象,当您将鼠标悬停在该对象上时,您可以在该对象的“选定”属性中找到该选定特征。在这种情况下,当您将鼠标悬停在外时,相同的对象将在“取消选择”属性中可用,直到您选择一个新对象
【讨论】:
【参考方案3】:This example from a userlist 非常有助于展示如何将悬停和点击事件分配给矢量图层。
【讨论】:
【参考方案4】:这对我有用。最终很简单,但花了一段时间:
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 微秒延迟 - 可能会更短,具体取决于您的使用情况。
【讨论】:
【参考方案5】:我使用一个函数来添加它,这里是一个简化的版本。请注意,您设置了详细信息并调用了底部的函数。另请注意,您不能在多个图层上拥有多个选择器 - 我认为只有添加的最后一个才会起作用,因此如果您希望在一个图层中有多个功能,则必须对其进行调整:
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);
【讨论】:
【参考方案6】:对于如何做到这一点的简单示例,您需要做几件事:
创建一个矢量图层以包含您的标记并将其添加到地图中:
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);
, // ...
【讨论】:
更好的解决方案,而不是向添加到图层的每个标记添加事件。【参考方案7】:您需要使用 selectControl 来创建弹出窗口。为了让它响应悬停而不是点击在构造函数中设置 hover:true。
【讨论】:
【参考方案8】:听起来你想看看“OpenLayers.Popup()”
此示例在您绘制多边形后显示一个弹出窗口,然后单击它,但您应该能够将其更改为响应悬停。 http://openlayers.org/dev/examples/select-feature-openpopup.html
【讨论】:
以上是关于openlayers简单的鼠标悬停在标记上的主要内容,如果未能解决你的问题,请参考以下文章
在 Google Maps V3 API 标记鼠标悬停时获取鼠标光标的位置