SelectFeature
类中的Control
方法提供了一种通过分别监听事件featureselected
和featureunselected
来添加和删除Vector层上弹出窗口的方法。下面显示了我在openlayers网站上从an example获得的示例代码:
// create the layer with listeners to create and destroy popups
var vector = new OpenLayers.Layer.Vector("Points",{
eventListeners:{
'featureselected':function(evt){
var feature = evt.feature;
var popup = new OpenLayers.Popup.FramedCloud("popup",
OpenLayers.LonLat.fromString(feature.geometry.toShortString()),
null,
"<div style='font-size:.8em'>Feature: " + feature.id +"<br>Foo: </div>",
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);
// create the select feature control
var selector = new OpenLayers.Control.SelectFeature(vector,{
hover:true, # this line
autoActivate:true
});
上面的代码将允许鼠标悬停在Geometry对象(地图上的图标或标记)上时显示弹出窗口。如果删除了行hover:true
,只有在鼠标单击几何对象时才会显示弹出窗口。
我想要的是能够在鼠标悬停时显示一种类型的弹出窗口(例如,图像加上标题),并且在鼠标单击时显示另一种类型(例如,详细说明)。我不确定如何做到这一点。一些帮助将非常感激。感谢。
答案 0 :(得分:3)
另外,还有另外一种方法,它比正确使用API更具攻击性,但似乎有效。您可以覆盖回调和回调。
var selectControl = new OpenLayers.Control.SelectFeature(vectorLayer, {
callbacks: {
over: function(feat) {
console.log('Show popup type 1');
},
out: function(feat) {
console.log('Hide popup type 1');
}
},
eventListeners: {
featurehighlighted: function(feat) {
console.log('Show popup type 2');
},
featureunhighlighted: function(feat) {
console.log('Hide popup type 2');
}
}
});
以下是工作示例:http://jsfiddle.net/eW8DV/1/
查看选择控件的来源以了解详细信息。