| 1 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
|---|
| 2 |
<head> |
|---|
| 3 |
<title>OpenLayers Click Handler Example</title> |
|---|
| 4 |
|
|---|
| 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"> |
|---|
| 8 |
#map { |
|---|
| 9 |
width: 340px; |
|---|
| 10 |
height: 170px; |
|---|
| 11 |
border: 1px solid gray; |
|---|
| 12 |
} |
|---|
| 13 |
#west { |
|---|
| 14 |
width: 350px; |
|---|
| 15 |
} |
|---|
| 16 |
#east { |
|---|
| 17 |
position: absolute; |
|---|
| 18 |
left: 370px; |
|---|
| 19 |
top: 3em; |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
table td { |
|---|
| 23 |
text-align: center; |
|---|
| 24 |
margin: 0; |
|---|
| 25 |
border: 1px solid gray; |
|---|
| 26 |
} |
|---|
| 27 |
textarea.output { |
|---|
| 28 |
text-align: left; |
|---|
| 29 |
font-size: 0.9em; |
|---|
| 30 |
width: 250px; |
|---|
| 31 |
height: 65px; |
|---|
| 32 |
overflow: auto; |
|---|
| 33 |
} |
|---|
| 34 |
</style> |
|---|
| 35 |
<script src="../lib/Firebug/firebug.js"></script> |
|---|
| 36 |
<script src="../lib/OpenLayers.js"></script> |
|---|
| 37 |
<script type="text/javascript"> |
|---|
| 38 |
|
|---|
| 39 |
OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, { |
|---|
| 40 |
defaultHandlerOptions: { |
|---|
| 41 |
'single': true, |
|---|
| 42 |
'double': false, |
|---|
| 43 |
'pixelTolerance': 0, |
|---|
| 44 |
'stopSingle': false, |
|---|
| 45 |
'stopDouble': false |
|---|
| 46 |
}, |
|---|
| 47 |
|
|---|
| 48 |
initialize: function(options) { |
|---|
| 49 |
this.handlerOptions = OpenLayers.Util.extend( |
|---|
| 50 |
{}, this.defaultHandlerOptions |
|---|
| 51 |
); |
|---|
| 52 |
OpenLayers.Control.prototype.initialize.apply( |
|---|
| 53 |
this, arguments |
|---|
| 54 |
); |
|---|
| 55 |
this.handler = new OpenLayers.Handler.Click( |
|---|
| 56 |
this, { |
|---|
| 57 |
'click': this.onClick, |
|---|
| 58 |
'dblclick': this.onDblclick |
|---|
| 59 |
}, this.handlerOptions |
|---|
| 60 |
); |
|---|
| 61 |
}, |
|---|
| 62 |
|
|---|
| 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"; |
|---|
| 67 |
}, |
|---|
| 68 |
|
|---|
| 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"; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
}); |
|---|
| 76 |
|
|---|
| 77 |
var map, controls; |
|---|
| 78 |
|
|---|
| 79 |
function init(){ |
|---|
| 80 |
|
|---|
| 81 |
map = new OpenLayers.Map('map'); |
|---|
| 82 |
var layer = new OpenLayers.Layer.WMS( |
|---|
| 83 |
"OpenLayers WMS", |
|---|
| 84 |
"http://labs.metacarta.com/wms/vmap0", |
|---|
| 85 |
{layers: 'basic'} |
|---|
| 86 |
); |
|---|
| 87 |
map.addLayers([layer]); |
|---|
| 88 |
|
|---|
| 89 |
controls = { |
|---|
| 90 |
"single": new OpenLayers.Control.Click({ |
|---|
| 91 |
hanlerOptions: { |
|---|
| 92 |
"single": true |
|---|
| 93 |
} |
|---|
| 94 |
}), |
|---|
| 95 |
"double": new OpenLayers.Control.Click({ |
|---|
| 96 |
handlerOptions: { |
|---|
| 97 |
"single": false, |
|---|
| 98 |
"double": true |
|---|
| 99 |
} |
|---|
| 100 |
}), |
|---|
| 101 |
"both": new OpenLayers.Control.Click({ |
|---|
| 102 |
handlerOptions: { |
|---|
| 103 |
"single": true, |
|---|
| 104 |
"double": true |
|---|
| 105 |
} |
|---|
| 106 |
}), |
|---|
| 107 |
"drag": new OpenLayers.Control.Click({ |
|---|
| 108 |
handlerOptions: { |
|---|
| 109 |
"single": true, |
|---|
| 110 |
"pixelTolerance": null |
|---|
| 111 |
} |
|---|
| 112 |
}), |
|---|
| 113 |
"stopsingle": new OpenLayers.Control.Click({ |
|---|
| 114 |
handlerOptions: { |
|---|
| 115 |
"single": true, |
|---|
| 116 |
"stopSingle": true |
|---|
| 117 |
} |
|---|
| 118 |
}), |
|---|
| 119 |
"stopdouble": new OpenLayers.Control.Click({ |
|---|
| 120 |
handlerOptions: { |
|---|
| 121 |
"single": false, |
|---|
| 122 |
"double": true, |
|---|
| 123 |
"stopDouble": true |
|---|
| 124 |
} |
|---|
| 125 |
}) |
|---|
| 126 |
}; |
|---|
| 127 |
|
|---|
| 128 |
var props = document.getElementById("props"); |
|---|
| 129 |
var control; |
|---|
| 130 |
for(var key in controls) { |
|---|
| 131 |
control = controls[key]; |
|---|
| 132 |
|
|---|
| 133 |
control.key = key; |
|---|
| 134 |
map.addControl(control); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
map.zoomToMaxExtent(); |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
function toggle(key) { |
|---|
| 141 |
var control = controls[key]; |
|---|
| 142 |
if(control.active) { |
|---|
| 143 |
control.deactivate(); |
|---|
| 144 |
} else { |
|---|
| 145 |
control.activate(); |
|---|
| 146 |
} |
|---|
| 147 |
var status = document.getElementById(key + "Status"); |
|---|
| 148 |
status.innerHTML = control.active ? "on" : "off"; |
|---|
| 149 |
var output = document.getElementById(key + "Output"); |
|---|
| 150 |
output.value = ""; |
|---|
| 151 |
} |
|---|
| 152 |
</script> |
|---|
| 153 |
</head> |
|---|
| 154 |
<body onload="init()"> |
|---|
| 155 |
<h1 id="title">Click Handler Example</h1> |
|---|
| 156 |
<div id="west"> |
|---|
| 157 |
|
|---|
| 158 |
<div id="tags"> |
|---|
| 159 |
</div> |
|---|
| 160 |
|
|---|
| 161 |
<p id="shortdesc"> |
|---|
| 162 |
This example shows the use of the click handler. |
|---|
| 163 |
</p> |
|---|
| 164 |
|
|---|
| 165 |
<div id="map" class="smallmap"></div> |
|---|
| 166 |
<p> |
|---|
| 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. |
|---|
| 174 |
</p> |
|---|
| 175 |
<p> |
|---|
| 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 |
|---|
| 186 |
zooming in. |
|---|
| 187 |
</p> |
|---|
| 188 |
</div> |
|---|
| 189 |
<div id="east"> |
|---|
| 190 |
<table> |
|---|
| 191 |
<caption>Controls with click handlers (toggle on/off to clear output)</caption> |
|---|
| 192 |
<tbody> |
|---|
| 193 |
<tr> |
|---|
| 194 |
<td>single only</td> |
|---|
| 195 |
<td><button id="singleStatus" onclick="toggle('single')">off</button></td> |
|---|
| 196 |
<td><textarea class="output" id="singleOutput"></textarea></td> |
|---|
| 197 |
</tr> |
|---|
| 198 |
<tr> |
|---|
| 199 |
<td>double only</td> |
|---|
| 200 |
<td><button id="doubleStatus" onclick="toggle('double')">off</button></td> |
|---|
| 201 |
<td><textarea class="output" id="doubleOutput"></textarea></td> |
|---|
| 202 |
</tr> |
|---|
| 203 |
<tr> |
|---|
| 204 |
<td>both</td> |
|---|
| 205 |
<td><button id="bothStatus" onclick="toggle('both')">off</button></td> |
|---|
| 206 |
<td><textarea class="output" id="bothOutput"></textarea></td> |
|---|
| 207 |
</tr> |
|---|
| 208 |
<tr> |
|---|
| 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> |
|---|
| 212 |
</tr> |
|---|
| 213 |
<tr> |
|---|
| 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> |
|---|
| 217 |
</tr> |
|---|
| 218 |
<tr> |
|---|
| 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> |
|---|
| 222 |
</tr> |
|---|
| 223 |
</tbody> |
|---|
| 224 |
</table> |
|---|
| 225 |
</div> |
|---|
| 226 |
</body> |
|---|
| 227 |
</html> |
|---|