1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Control/ZoomBox.js
7 * @requires OpenLayers/Control/DragPan.js
8 * @requires OpenLayers/Handler/MouseWheel.js
9 * @requires OpenLayers/Handler/Click.js
13 * Class: OpenLayers.Control.Navigation
14 * The navigation control handles map browsing with mouse events (dragging,
15 * double-clicking, and scrolling the wheel). Create a new navigation
16 * control with the <OpenLayers.Control.Navigation> control.
18 * Note that this control is added to the map by default (if no controls
19 * array is sent in the options object to the <OpenLayers.Map>
23 * - <OpenLayers.Control>
25 OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, {
29 * {<OpenLayers.Control.DragPan>}
34 * APIProprety: dragPanOptions
35 * {Object} Options passed to the DragPan control.
41 * {<OpenLayers.Control.ZoomBox>}
46 * APIProperty: zoomWheelEnabled
47 * {Boolean} Whether the mousewheel should zoom the map
49 zoomWheelEnabled: true,
52 * APIProperty: handleRightClicks
53 * {Boolean} Whether or not to handle right clicks. Default is false.
55 handleRightClicks: false,
58 * APIProperty: zoomBoxKeyMask
59 * {Integer} <OpenLayers.Handler> key code of the key, which has to be
60 * pressed, while drawing the zoom box with the mouse on the screen.
61 * You should probably set handleRightClicks to true if you use this
62 * with MOD_CTRL, to disable the context menu for machines which use
63 * CTRL-Click as a right click.
64 * Default: <OpenLayers.Handler.MOD_SHIFT
66 zoomBoxKeyMask: OpenLayers.Handler.MOD_SHIFT,
69 * Constructor: OpenLayers.Control.Navigation
70 * Create a new navigation control
73 * options - {Object} An optional object whose properties will be set on
76 initialize: function(options) {
78 OpenLayers.Control.prototype.initialize.apply(this, arguments);
83 * The destroy method is used to perform any clean up before the control
84 * is dereferenced. Typically this is where event listeners are removed
85 * to prevent memory leaks.
91 this.dragPan.destroy();
96 this.zoomBox.destroy();
99 OpenLayers.Control.prototype.destroy.apply(this,arguments);
105 activate: function() {
106 this.dragPan.activate();
107 if (this.zoomWheelEnabled) {
108 this.handlers.wheel.activate();
110 this.handlers.click.activate();
111 this.zoomBox.activate();
112 return OpenLayers.Control.prototype.activate.apply(this,arguments);
118 deactivate: function() {
119 this.zoomBox.deactivate();
120 this.dragPan.deactivate();
121 this.handlers.click.deactivate();
122 this.handlers.wheel.deactivate();
123 return OpenLayers.Control.prototype.deactivate.apply(this,arguments);
130 // disable right mouse context menu for support of right click events
131 if (this.handleRightClicks) {
132 this.map.viewPortDiv.oncontextmenu = function () { return false;};
135 var clickCallbacks = {
136 'dblclick': this.defaultDblClick,
137 'dblrightclick': this.defaultDblRightClick
143 this.handlers.click = new OpenLayers.Handler.Click(
144 this, clickCallbacks, clickOptions
146 this.dragPan = new OpenLayers.Control.DragPan(
147 OpenLayers.Util.extend({map: this.map}, this.dragPanOptions)
149 this.zoomBox = new OpenLayers.Control.ZoomBox(
150 {map: this.map, keyMask: this.zoomBoxKeyMask});
153 this.handlers.wheel = new OpenLayers.Handler.MouseWheel(
154 this, {"up" : this.wheelUp,
155 "down": this.wheelDown} );
160 * Method: defaultDblClick
165 defaultDblClick: function (evt) {
166 var newCenter = this.map.getLonLatFromViewPortPx( evt.xy );
167 this.map.setCenter(newCenter, this.map.zoom + 1);
171 * Method: defaultDblRightClick
176 defaultDblRightClick: function (evt) {
177 var newCenter = this.map.getLonLatFromViewPortPx( evt.xy );
178 this.map.setCenter(newCenter, this.map.zoom - 1);
182 * Method: wheelChange
188 wheelChange: function(evt, deltaZ) {
189 var newZoom = this.map.getZoom() + deltaZ;
190 if (!this.map.isValidZoomLevel(newZoom)) {
193 var size = this.map.getSize();
194 var deltaX = size.w/2 - evt.xy.x;
195 var deltaY = evt.xy.y - size.h/2;
196 var newRes = this.map.baseLayer.getResolutionForZoom(newZoom);
197 var zoomPoint = this.map.getLonLatFromPixel(evt.xy);
198 var newCenter = new OpenLayers.LonLat(
199 zoomPoint.lon + deltaX * newRes,
200 zoomPoint.lat + deltaY * newRes );
201 this.map.setCenter( newCenter, newZoom );
206 * User spun scroll wheel up
211 wheelUp: function(evt) {
212 this.wheelChange(evt, 1);
217 * User spun scroll wheel down
222 wheelDown: function(evt) {
223 this.wheelChange(evt, -1);
227 * Method: disableZoomWheel
230 disableZoomWheel : function() {
231 this.zoomWheelEnabled = false;
232 this.handlers.wheel.deactivate();
236 * Method: enableZoomWheel
239 enableZoomWheel : function() {
240 this.zoomWheelEnabled = true;
242 this.handlers.wheel.activate();
246 CLASS_NAME: "OpenLayers.Control.Navigation"