Vector OpenLayers上只有一个弹出窗口
我使用带有矢量层的OpenLayers在地图上显示不同的项目 最重要的是,我想为每个项目(一个功能)添加一个弹出窗口(当单击项目时显示弹出窗口)。为此,我必须:Vector OpenLayers上只有一个弹出窗口,openlayers,openstreetmap,Openlayers,Openstreetmap,我使用带有矢量层的OpenLayers在地图上显示不同的项目 最重要的是,我想为每个项目(一个功能)添加一个弹出窗口(当单击项目时显示弹出窗口)。为此,我必须: function initMap() { // In this function I add with success the different items to the vectorLayer. } function finishMap() { map.addLayer(
function initMap()
{
// In this function I add with success the different items to the vectorLayer.
}
function finishMap()
{
map.addLayer(vectorLayer);
selectControl = new OpenLayers.Control.SelectFeature(vectorLayer,
{
onSelect: onFeatureSelect,
onUnselect: onFeatureUnselect
});
map.addControl(selectControl);
selectControl.activate();
}
function onFeatureClose(evt) {
selectControl.unselect(selectedFeature);
}
function onFeatureSelect(feature) {
var popup = new OpenLayers.Popup.FramedCloud("popup",
feature.geometry.getBounds().getCenterLonLat(),
null,
feature.description,
true,
onFeatureClose);
popup.panMapIfOutOfView = true;
popup.autoSize = true;
feature.popup = popup;
map.addPopup(popup);
}
function onFeatureUnselect(feature) {
map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
}
对不同功能的调用是:
问题是:我只有一个项目(超过10个)通过单击它会弹出一个窗口…一般来说,将选择处理程序直接实现到层对象更容易,这是(我猜)在initMap方法中。使用eventListeners属性,如下所示:
var layer = new OpenLayers.Layer.Vector("Vector layer", {
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: " + feature.attributes.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;
}
}
});
//create selector control
var selector = new OpenLayers.Control.SelectFeature(layer,{
autoActivate:true
});
var-layer=new OpenLayers.layer.Vector(“向量层”,{
eventListeners:{
“featureselected”:函数(evt){
var特征=evt特征;
var popup=new OpenLayers.popup.FramedCloud(“popup”,
OpenLayers.LonLat.fromString(feature.geometry.toShortString()),
无效的
“功能:“+Feature.id+”
Foo:“+Feature.attributes.Foo+”,
无效的
真的
);
feature.popup=弹出窗口;
map.addPopup(弹出窗口);
},
“featureunselected”:函数(evt){
var特征=evt特征;
map.removeppopup(feature.popup);
feature.popup.destroy();
feature.popup=null;
}
}
});
//创建选择器控件
var选择器=新建OpenLayers.Control.SelectFeature(图层{
自动激活:真
});
示例实现:唯一的区别是selectr对mouseover和mouseout作出反应,而不是单击(这是通过将选择器的hover属性设置为true来完成的)
还有一个非常类似的问题:
有关这些类的更多详细信息,请参见OL文档:
或者直接问
希望这些至少能有所帮助