Если вы реализуете решение, в то время как только одно всплывающее окно активно одновременно (т.е. каждый раз, когда всплывающее окно не выбрано, оно исчезает), вы НИКОГДА не будете иметь более одного всплывающего окна за раз.
прочитайте этот STACKOVERFLOW ответ, который я написал именно для этой проблемы. у вас есть весь необходимый псевдокод (с подробными объяснениями обо всем).
если вам не нужны объяснения, это показывает решение:
var urlKML = 'parseKMLTrack07d.php';
var layerKML = new OpenLayers.Layer.Vector("KML1", {
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: urlKML,
format: new OpenLayers.Format.KML({
extractStyles: true,
extractAttributes: true,
maxDepth: 2
})
})
});
var layerOSM = new OpenLayers.Layer.OSM();
var map = new OpenLayers.Map({
div: "map",
layers: [
layerOSM,
layerKML
]
});
var selectStop = new OpenLayers.Control.SelectFeature(layerKML,{onSelect: onFeatureSelect, onUnselect: onFeatureUnselect});
layerKML.events.on({
"featureselected": onFeatureSelect,
"featureunselected": onFeatureUnselect
});
map.addControl(selectStop);
selectStop.activate();
function onFeatureSelect(event) {
var feature = event.feature;
var content = feature.attributes.name + '<br/>'+feature.attributes.description;
popup = new OpenLayers.Popup.FramedCloud("chicken",
feature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size(100,100),
content,
null, true, onFeatureUnselect);
feature.popup = popup;
map.addPopup(popup);
// GLOBAL variable, in case popup is destroyed by clicking CLOSE box
lastfeature = feature;
}
function onFeatureUnselect(event) {
var feature = lastfeature;
if(feature.popup) {
map.removePopup(feature.popup);
feature.popup.destroy();
delete feature.popup;
}
}
Теперь, если вы ДЕЙСТВИТЕЛЬНО хотите уничтожить все всплывающие окна, независимо (чего я очень не одобряю):
function popupClear() {
//alert('number of popups '+map.popups.length);
while( map.popups.length ) {
map.removePopup(map.popups[0]);
}
}