1 <html xmlns="http://www.w3.org/1999/xhtml">
3 <title>Open Popup on Layer.Vector</title>
4 <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
5 <link rel="stylesheet" href="style.css" type="text/css" />
6 <style type="text/css">
11 <script src="../lib/OpenLayers.js"></script>
12 <script type="text/javascript">
13 var map, drawControls, selectControl, selectedFeature;
14 function onPopupClose(evt) {
15 selectControl.unselect(selectedFeature);
17 function onFeatureSelect(feature) {
18 selectedFeature = feature;
19 popup = new OpenLayers.Popup.FramedCloud("chicken",
20 feature.geometry.getBounds().getCenterLonLat(),
22 "<div style='font-size:.8em'>Feature: " + feature.id +"<br />Area: " + feature.geometry.getArea()+"</div>",
23 null, true, onPopupClose);
24 feature.popup = popup;
27 function onFeatureUnselect(feature) {
28 map.removePopup(feature.popup);
29 feature.popup.destroy();
33 map = new OpenLayers.Map('map');
34 var wmsLayer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
35 "http://labs.metacarta.com/wms/vmap0?", {layers: 'basic'});
37 var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");
39 map.addLayers([wmsLayer, polygonLayer]);
40 map.addControl(new OpenLayers.Control.LayerSwitcher());
41 map.addControl(new OpenLayers.Control.MousePosition());
43 selectControl = new OpenLayers.Control.SelectFeature(polygonLayer,
44 {onSelect: onFeatureSelect, onUnselect: onFeatureUnselect});
46 polygon: new OpenLayers.Control.DrawFeature(polygonLayer,
47 OpenLayers.Handler.Polygon),
51 for(var key in drawControls) {
52 map.addControl(drawControls[key]);
56 map.setCenter(new OpenLayers.LonLat(0, 0), 3);
60 function toggleControl(element) {
61 for(key in drawControls) {
62 var control = drawControls[key];
63 if(element.value == key && element.checked) {
72 <body onload="init()">
73 <h1 id="title">Open Popup on Layer.Vector</h1>
75 Using a Control.SelectFeature, open a popup on click.
77 <div id="map" class="smallmap"></div>
78 <ul id="controlToggle">
80 <input type="radio" name="type" value="none" id="noneToggle"
81 onclick="toggleControl(this);" checked="checked" />
82 <label for="noneToggle">navigate</label>
85 <input type="radio" name="type" value="polygon" id="polygonToggle"
86 onclick="toggleControl(this);" />
87 <label for="polygonToggle">draw polygon</label>
90 <input type="radio" name="type" value="select" id="selectToggle"
91 onclick="toggleControl(this);" />
92 <label for="selectToggle">select polygon on click</label>
95 <p>It is possible to use the onSelect/onUnselect hooks on the SelectFeature
96 to do fun things -- like open a popup.