diff --git a/examples/sundials.html b/examples/sundials.html index 47ee24244c..d7fd014e5d 100644 --- a/examples/sundials.html +++ b/examples/sundials.html @@ -17,33 +17,47 @@ var lon = 5; var lat = 40; var zoom = 5; - var map, layer; + var map, select; function init(){ map = new OpenLayers.Map('map'); - layer = new OpenLayers.Layer.WMS( "OpenLayers WMS", - "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} ); - map.addLayer(layer); - map.addLayer(new OpenLayers.Layer.GML("KML", "kml/sundials.kml", - { - format: OpenLayers.Format.KML, - formatOptions: { - extractStyles: true, - extractAttributes: true - } - })); - selectControl = new OpenLayers.Control.SelectFeature(map.layers[1], - {onSelect: onFeatureSelect, onUnselect: onFeatureUnselect}); - - map.addControl(selectControl); - selectControl.activate(); + + var wms = new OpenLayers.Layer.WMS( + "OpenLayers WMS", + "http://labs.metacarta.com/wms/vmap0", + {layers: 'basic'} + ); + + var sundials = new OpenLayers.Layer.Vector("KML", { + projection: map.displayProjection, + strategies: [new OpenLayers.Strategy.Fixed()], + protocol: new OpenLayers.Protocol.HTTP({ + url: "kml/sundials.kml", + format: new OpenLayers.Format.KML({ + extractStyles: true, + extractAttributes: true + }) + }) + }); + + map.addLayers([wms, sundials]); + + select = new OpenLayers.Control.SelectFeature(sundials); + + sundials.events.on({ + "featureselected": onFeatureSelect, + "featureunselected": onFeatureUnselect + }); + + map.addControl(select); + select.activate(); map.zoomToExtent(new OpenLayers.Bounds(68.774414,11.381836,123.662109,34.628906)); } function onPopupClose(evt) { - selectControl.unselect(selectedFeature); + select.unselectAll(); } - function onFeatureSelect(feature) { - selectedFeature = feature; + function onFeatureSelect(event) { + var feature = event.feature; // Since KML is user-generated, do naive protection against // Javascript. var content = "

"+feature.attributes.name + "

" + feature.attributes.description; @@ -58,10 +72,13 @@ feature.popup = popup; map.addPopup(popup); } - function onFeatureUnselect(feature) { - map.removePopup(feature.popup); - feature.popup.destroy(); - feature.popup = null; + function onFeatureUnselect(event) { + var feature = event.feature; + if(feature.popup) { + map.removePopup(feature.popup); + feature.popup.destroy(); + delete feature.popup; + } }