1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>OpenLayers Click Handler Example</title> <link rel="stylesheet" href="../theme/default/style.css" type="text/css" /> <link rel="stylesheet" href="style.css" type="text/css" /> <style type="text/css"> #map { width: 340px; height: 170px; border: 1px solid gray; } #west { width: 350px; } #east { position: absolute; left: 370px; top: 3em; } table td { text-align: center; margin: 0; border: 1px solid gray; } textarea.output { text-align: left; font-size: 0.9em; width: 250px; height: 65px; overflow: auto; } </style> <script src="../lib/Firebug/firebug.js"></script> <script src="../lib/OpenLayers.js"></script> <script type="text/javascript"> OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, { defaultHandlerOptions: { 'single': true, 'double': false, 'pixelTolerance': 0, 'stopSingle': false, 'stopDouble': false }, initialize: function(options) { this.handlerOptions = OpenLayers.Util.extend( {}, this.defaultHandlerOptions ); OpenLayers.Control.prototype.initialize.apply( this, arguments ); this.handler = new OpenLayers.Handler.Click( this, { 'click': this.onClick, 'dblclick': this.onDblclick }, this.handlerOptions ); }, onClick: function(evt) { var output = document.getElementById(this.key + "Output"); var msg = "click " + evt.xy; output.value = output.value + msg + "\r\n"; }, onDblclick: function(evt) { var output = document.getElementById(this.key + "Output"); var msg = "dblclick " + evt.xy; output.value = output.value + msg + "\n"; } }); var map, controls; function init(){ map = new OpenLayers.Map('map'); var layer = new OpenLayers.Layer.WMS( "OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} ); map.addLayers([layer]); controls = { "single": new OpenLayers.Control.Click({ handlerOptions: { "single": true } }), "double": new OpenLayers.Control.Click({ handlerOptions: { "single": false, "double": true } }), "both": new OpenLayers.Control.Click({ handlerOptions: { "single": true, "double": true } }), "drag": new OpenLayers.Control.Click({ handlerOptions: { "single": true, "pixelTolerance": null } }), "stopsingle": new OpenLayers.Control.Click({ handlerOptions: { "single": true, "stopSingle": true } }), "stopdouble": new OpenLayers.Control.Click({ handlerOptions: { "single": false, "double": true, "stopDouble": true } }) }; var props = document.getElementById("props"); var control; for(var key in controls) { control = controls[key]; // only to route output here control.key = key; map.addControl(control); } map.zoomToMaxExtent(); } function toggle(key) { var control = controls[key]; if(control.active) { control.deactivate(); } else { control.activate(); } var status = document.getElementById(key + "Status"); status.innerHTML = control.active ? "on" : "off"; var output = document.getElementById(key + "Output"); output.value = ""; } </script> </head> <body onload="init()"> <h1 id="title">Click Handler Example</h1> <div id="west"> <div id="tags"> </div> <p id="shortdesc"> This example shows the use of the click handler. </p> <div id="map" class="smallmap"></div> <p> The click handler can be used to gain more flexibility over handling click events. The handler can be constructed with options to handle only single click events, to handle single and double-click events, to ignore clicks that include a drag, and to stop propagation of single and/or double-click events. A single click is a click that is not followed by another click for more than 300ms. This delay is configured with the delay property. </p> <p> The options to stop single and double clicks have to do with stopping event propagation on the map events listener queue (not stopping events from cascading to other elements). The ability to stop an event from propagating has to do with the order in which listeners are registered. With stopSingle or stopDouble true, a click handler will stop propagation to all listeners that were registered (or all handlers that were activated) before the click handler was activated. So, for example, activating a click handler with stopDouble true after the navigation control is active will stop double-clicks from zooming in. </p> </div> <div id="east"> <table> <caption>Controls with click handlers (toggle on/off to clear output)</caption> <tbody> <tr> <td>single only</td> <td><button id="singleStatus" onclick="toggle('single')">off</button></td> <td><textarea class="output" id="singleOutput"></textarea></td> </tr> <tr> <td>double only</td> <td><button id="doubleStatus" onclick="toggle('double')">off</button></td> <td><textarea class="output" id="doubleOutput"></textarea></td> </tr> <tr> <td>both</td> <td><button id="bothStatus" onclick="toggle('both')">off</button></td> <td><textarea class="output" id="bothOutput"></textarea></td> </tr> <tr> <td>single with drag</td> <td><button id="dragStatus" onclick="toggle('drag')">off</button></td> <td><textarea class="output" id="dragOutput"></textarea></td> </tr> <tr> <td>single with stop</td> <td><button id="stopsingleStatus" onclick="toggle('stopsingle')">off</button></td> <td><textarea class="output" id="stopsingleOutput"></textarea></td> </tr> <tr> <td>double with stop</td> <td><button id="stopdoubleStatus" onclick="toggle('stopdouble')">off</button></td> <td><textarea class="output" id="stopdoubleOutput"></textarea></td> </tr> </tbody> </table> </div> </body> </html> |