1 <html xmlns="http://www.w3.org/1999/xhtml">
3 <title>OpenLayers Click Handler Example</title>
5 <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
6 <link rel="stylesheet" href="style.css" type="text/css" />
7 <style type="text/css">
11 border: 1px solid gray;
25 border: 1px solid gray;
35 <script src="../lib/Firebug/firebug.js"></script>
36 <script src="../lib/OpenLayers.js"></script>
37 <script type="text/javascript">
39 OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
40 defaultHandlerOptions: {
48 initialize: function(options) {
49 this.handlerOptions = OpenLayers.Util.extend(
50 {}, this.defaultHandlerOptions
52 OpenLayers.Control.prototype.initialize.apply(
55 this.handler = new OpenLayers.Handler.Click(
57 'click': this.onClick,
58 'dblclick': this.onDblclick
59 }, this.handlerOptions
63 onClick: function(evt) {
64 var output = document.getElementById(this.key + "Output");
65 var msg = "click " + evt.xy;
66 output.value = output.value + msg + "\r\n";
69 onDblclick: function(evt) {
70 var output = document.getElementById(this.key + "Output");
71 var msg = "dblclick " + evt.xy;
72 output.value = output.value + msg + "\n";
81 map = new OpenLayers.Map('map');
82 var layer = new OpenLayers.Layer.WMS(
84 "http://labs.metacarta.com/wms/vmap0",
87 map.addLayers([layer]);
90 "single": new OpenLayers.Control.Click({
95 "double": new OpenLayers.Control.Click({
101 "both": new OpenLayers.Control.Click({
107 "drag": new OpenLayers.Control.Click({
110 "pixelTolerance": null
113 "stopsingle": new OpenLayers.Control.Click({
119 "stopdouble": new OpenLayers.Control.Click({
128 var props = document.getElementById("props");
130 for(var key in controls) {
131 control = controls[key];
132 // only to route output here
134 map.addControl(control);
137 map.zoomToMaxExtent();
140 function toggle(key) {
141 var control = controls[key];
143 control.deactivate();
147 var status = document.getElementById(key + "Status");
148 status.innerHTML = control.active ? "on" : "off";
149 var output = document.getElementById(key + "Output");
154 <body onload="init()">
155 <h1 id="title">Click Handler Example</h1>
162 This example shows the use of the click handler.
165 <div id="map" class="smallmap"></div>
167 The click handler can be used to gain more flexibility over handling
168 click events. The handler can be constructed with options to handle
169 only single click events, to handle single and double-click events,
170 to ignore clicks that include a drag, and to stop propagation of
171 single and/or double-click events. A single click is a click that
172 is not followed by another click for more than 300ms. This delay
173 is configured with the delay property.
176 The options to stop single and double clicks have to do with
177 stopping event propagation on the map events listener queue
178 (not stopping events from cascading to other elements). The
179 ability to stop an event from propagating has to do with the
180 order in which listeners are registered. With stopSingle or
181 stopDouble true, a click handler will stop propagation to all
182 listeners that were registered (or all handlers that were
183 activated) before the click handler was activated. So, for
184 example, activating a click handler with stopDouble true after
185 the navigation control is active will stop double-clicks from
191 <caption>Controls with click handlers (toggle on/off to clear output)</caption>
195 <td><button id="singleStatus" onclick="toggle('single')">off</button></td>
196 <td><textarea class="output" id="singleOutput"></textarea></td>
200 <td><button id="doubleStatus" onclick="toggle('double')">off</button></td>
201 <td><textarea class="output" id="doubleOutput"></textarea></td>
205 <td><button id="bothStatus" onclick="toggle('both')">off</button></td>
206 <td><textarea class="output" id="bothOutput"></textarea></td>
209 <td>single with drag</td>
210 <td><button id="dragStatus" onclick="toggle('drag')">off</button></td>
211 <td><textarea class="output" id="dragOutput"></textarea></td>
214 <td>single with stop</td>
215 <td><button id="stopsingleStatus" onclick="toggle('stopsingle')">off</button></td>
216 <td><textarea class="output" id="stopsingleOutput"></textarea></td>
219 <td>double with stop</td>
220 <td><button id="stopdoubleStatus" onclick="toggle('stopdouble')">off</button></td>
221 <td><textarea class="output" id="stopdoubleOutput"></textarea></td>