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
|
var map, navigationControl, queryControl;
function init(){
map = new OpenLayers.Map('map', {controls: []});
var layer = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0",
{layers: 'basic'}
);
map.addLayers([layer]);
navigationControl = new OpenLayers.Control.KeyboardDefaults({
observeElement: 'map'
});
map.addControl(navigationControl);
queryControl = new OpenLayers.Control.KeyboardClick({
observeElement: 'map'
});
map.addControl(queryControl);
map.zoomToMaxExtent();
}
/**
* Class: OpenLayers.Control.KeyboardClick
*
* A custom control that (a) adds a vector point that can be moved using the
* arrow keys of the keyboard, and (b) displays a browser alert window when the
* RETURN key is pressed. The control can be activated/deactivated using the
* "i" key. When activated the control deactivates any KeyboardDefaults control
* in the map so that the map is not moved when the arrow keys are pressed.
*
* This control relies on the OpenLayers.Handler.KeyboardPoint custom handler.
*/
OpenLayers.Control.KeyboardClick = OpenLayers.Class(OpenLayers.Control, {
initialize: function(options) {
OpenLayers.Control.prototype.initialize.apply(this, [options]);
var observeElement = this.observeElement || document;
this.handler = new OpenLayers.Handler.KeyboardPoint(this, {
done: this.onClick,
cancel: this.deactivate
}, {
observeElement: observeElement
});
OpenLayers.Event.observe(
observeElement,
"keydown",
OpenLayers.Function.bindAsEventListener(
function(evt) {
if (evt.keyCode == 73) { // "i"
if (this.active) {
this.deactivate();
} else {
this.activate();
}
}
},
this
)
);
},
onClick: function(geometry) {
alert("You clicked near " + geometry.x + " N, " +
geometry.y + " E");
},
activate: function() {
if(!OpenLayers.Control.prototype.activate.apply(this, arguments)) {
return false;
}
// deactivate any KeyboardDefaults control
var keyboardDefaults = this.map.getControlsByClass(
'OpenLayers.Control.KeyboardDefaults')[0];
if (keyboardDefaults) {
keyboardDefaults.deactivate();
}
return true;
},
deactivate: function() {
if(!OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {
return false;
}
// reactivate any KeyboardDefaults control
var keyboardDefaults = this.map.getControlsByClass(
'OpenLayers.Control.KeyboardDefaults')[0];
if (keyboardDefaults) {
keyboardDefaults.activate();
}
return true;
}
});
/**
* Class: OpenLayers.Handler.KeyboardPoint
*
* A custom handler that displays a vector point that can be moved
* using the arrow keys of the keyboard.
*/
OpenLayers.Handler.KeyboardPoint = OpenLayers.Class(OpenLayers.Handler, {
KEY_EVENTS: ["keydown"],
initialize: function(control, callbacks, options) {
OpenLayers.Handler.prototype.initialize.apply(this, arguments);
// cache the bound event listener method so it can be unobserved later
this.eventListener = OpenLayers.Function.bindAsEventListener(
this.handleKeyEvent, this
);
},
activate: function() {
if(!OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
return false;
}
this.layer = new OpenLayers.Layer.Vector(this.CLASS_NAME);
this.map.addLayer(this.layer);
this.observeElement = this.observeElement || document;
for (var i=0, len=this.KEY_EVENTS.length; i<len; i++) {
OpenLayers.Event.observe(
this.observeElement, this.KEY_EVENTS[i], this.eventListener);
}
if(!this.point) {
this.createFeature();
}
return true;
},
deactivate: function() {
if (!OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
return false;
}
for (var i=0, len=this.KEY_EVENTS.length; i<len; i++) {
OpenLayers.Event.stopObserving(
this.observeElement, this.KEY_EVENTS[i], this.eventListener);
}
this.map.removeLayer(this.layer);
this.destroyFeature();
return true;
},
handleKeyEvent: function (evt) {
switch(evt.keyCode) {
case OpenLayers.Event.KEY_LEFT:
this.modifyFeature(-3, 0);
break;
case OpenLayers.Event.KEY_RIGHT:
this.modifyFeature(3, 0);
break;
case OpenLayers.Event.KEY_UP:
this.modifyFeature(0, 3);
break;
case OpenLayers.Event.KEY_DOWN:
this.modifyFeature(0, -3);
break;
case OpenLayers.Event.KEY_RETURN:
this.callback('done', [this.point.geometry.clone()]);
break;
case OpenLayers.Event.KEY_ESC:
this.callback('cancel');
break;
}
},
modifyFeature: function(lon, lat) {
if(!this.point) {
this.createFeature();
}
var resolution = this.map.getResolution();
this.point.geometry.x = this.point.geometry.x + lon * resolution;
this.point.geometry.y = this.point.geometry.y + lat * resolution;
this.callback("modify", [this.point.geometry, this.point, false]);
this.point.geometry.clearBounds();
this.drawFeature();
},
createFeature: function() {
var center = this.map.getCenter();
var geometry = new OpenLayers.Geometry.Point(
center.lon, center.lat
);
this.point = new OpenLayers.Feature.Vector(geometry);
this.callback("create", [this.point.geometry, this.point]);
this.point.geometry.clearBounds();
this.layer.addFeatures([this.point], {silent: true});
},
destroyFeature: function() {
this.layer.destroyFeatures([this.point]);
this.point = null;
},
drawFeature: function() {
this.layer.drawFeature(this.point, this.style);
}
});
|