From 4aef16e1bd1f9857fc7624e18407336d23709dce Mon Sep 17 00:00:00 2001 From: crschmidt Date: Fri, 6 Oct 2006 19:20:23 +0000 Subject: [PATCH] Jeff Dege contributed a MousePosition Control, which this commit adds to trunk. Reviewed by Erik on Trac and in person. git-svn-id: http://svn.openlayers.org/trunk/openlayers@1665 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- build/docs.sh | 2 +- doc/Control.MousePosition.txt | 32 +++++++ doc/authors.txt | 1 + examples/controls.html | 1 + lib/OpenLayers.js | 1 + lib/OpenLayers/Control/MousePosition.js | 106 ++++++++++++++++++++++++ theme/default/style.css | 8 ++ 7 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 doc/Control.MousePosition.txt create mode 100644 lib/OpenLayers/Control/MousePosition.js diff --git a/build/docs.sh b/build/docs.sh index 25f223693b..8752f3d5bf 100755 --- a/build/docs.sh +++ b/build/docs.sh @@ -1,7 +1,7 @@ #!/bin/sh rm ../doc/reference.html -CLASSES="Map Layer Layer.HTTPRequest Layer.Grid Layer.WMS Layer.KaMap Layer.EventPane Layer.Google Layer.VirtualEarth Layer.Markers Layer.Text Layer.GeoRSS Layer.Boxes Icon Marker Marker.Box Tile Tile.Image Tile.WFS Control Control.LayerSwitcher Control.MouseDefaults Control.MouseToolbar Control.PanZoom Control.PanZoomBar Control.Permalink Control.Scale LonLat Size Pixel Bounds Util" +CLASSES="Map Layer Layer.HTTPRequest Layer.Grid Layer.WMS Layer.KaMap Layer.EventPane Layer.Google Layer.VirtualEarth Layer.Markers Layer.Text Layer.GeoRSS Layer.Boxes Icon Marker Marker.Box Tile Tile.Image Tile.WFS Control Control.LayerSwitcher Control.MouseDefaults Control.MousePosition Control.MouseToolbar Control.PanZoom Control.PanZoomBar Control.Permalink Control.Scale LonLat Size Pixel Bounds Util" echo " OpenLayers Class Reference Documentation diff --git a/doc/Control.MousePosition.txt b/doc/Control.MousePosition.txt new file mode 100644 index 0000000000..9b3e8564ec --- /dev/null +++ b/doc/Control.MousePosition.txt @@ -0,0 +1,32 @@ +OpenLayers.Control.MousePosition + +A small control which displays the Longitude and Latitude of the current mouse position, by defualt in the lower right of the map viewport. + +* Constructor + OpenLayers.Control.MousePosition({Object|options}) -- Creates a new MousePosition control. + + +* Parameters + + element -- if not null, div in which to display the MousePosition + prefix -- html to precede the longitude value (default: '') + separator -- html to separate the longitude and latitude values (default: '
') + suffix -- html to follow the latitude value (default: '') + numdigits -- number of digits to the right of the decimal (default: 5) + granularity -- a change of how many pixels is considered a mouse move (default: 1) + +prefix, separator, and suffix are used to format the lon/lat values. + +With: + prefix = 'Lon: ' + suffix = '
Lat: ' + suffix = '' + nudigits = 3 +Lon/Lat is displayed as: + Lon: 95.123 + Lat: 35.456 + +If the mouse has never been over the map, Lon/Lat will equal 0/0. If the mouse is over the map, Lon/Lat will equal the current mouse position. If the mouse has been moved off the map, Lon/Lat will equal the value displayed at the time the mouse was moved off the map. + +If the mouse is moving slowly, the Lon/Lat will refresh continuously. If the mouse is moving rapidly, the refresh of Lon/Lat will be suspended until the mouse has slowed down or stopped. (Trying to update the Lon/Lat value while the mouse is in rapid movemement makes the movement of the mouse unacceptably jerky.) + diff --git a/doc/authors.txt b/doc/authors.txt index 51f630c770..fbcd848834 100644 --- a/doc/authors.txt +++ b/doc/authors.txt @@ -10,6 +10,7 @@ Patch contributors ------------------ Corey Puffault Tim Schaub +Jeff Dege OpenLayers is graciously supported by MetaCarta, Inc. . diff --git a/examples/controls.html b/examples/controls.html index 7f958def08..edb4924974 100644 --- a/examples/controls.html +++ b/examples/controls.html @@ -18,6 +18,7 @@ map.addControl(new OpenLayers.Control.LayerSwitcher({'ascending':false})); map.addControl(new OpenLayers.Control.Permalink()); map.addControl(new OpenLayers.Control.Permalink($('permalink'))); + map.addControl(new OpenLayers.Control.MousePosition()); var ol_wms = new OpenLayers.Layer.WMS( "OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} ); diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index d3e6c79c6c..4ab4bd003a 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -89,6 +89,7 @@ if (typeof(_OPENLAYERS_SFL_) == "undefined") { "OpenLayers/Control.js", "OpenLayers/Control/MouseDefaults.js", "OpenLayers/Control/MouseToolbar.js", + "OpenLayers/Control/MousePosition.js", "OpenLayers/Control/KeyboardDefaults.js", "OpenLayers/Control/PanZoom.js", "OpenLayers/Control/PanZoomBar.js", diff --git a/lib/OpenLayers/Control/MousePosition.js b/lib/OpenLayers/Control/MousePosition.js new file mode 100644 index 0000000000..84b5c60510 --- /dev/null +++ b/lib/OpenLayers/Control/MousePosition.js @@ -0,0 +1,106 @@ +/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license. + * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full + * text of the license. */ + +/** + * @class + * + * @requires OpenLayers/Control.js + */ +OpenLayers.Control.MousePosition = OpenLayers.Class.create(); +OpenLayers.Control.MousePosition.prototype = + OpenLayers.Class.inherit( OpenLayers.Control, { + + /** @type DOMElement */ + element: null, + + /** @type String */ + prefix: '', + + /** @type String */ + separator: ', ', + + /** @type String */ + suffix: '', + + /** @type int */ + numdigits: 5, + + /** @type int */ + granularity: 1, + + /** @type OpenLayers.LonLat */ + lastXy: null, + + /** + * @constructor + * + * @param {DOMElement} options Options for control. + */ + initialize: function(options) { + OpenLayers.Control.prototype.initialize.apply(this, arguments); + }, + + /** + * @type DOMElement + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + + if (!this.element) { + this.div.left = ""; + this.div.top = ""; + this.div.className = "olControlMousePosition"; + this.element = this.div; + } + + this.redraw(); + return this.div; + }, + + /** + * + */ + redraw: function(evt) { + + var lonLat; + + if (evt == null) { + lonLat = new OpenLayers.LonLat(0, 0); + } else { + if (this.lastXy == null || + Math.abs(evt.xy.x - this.lastXy.x) > this.granularity || + Math.abs(evt.xy.y - this.lastXy.y) > this.granularity) + { + this.lastXy = evt.xy; + return; + } + + lonLat = this.map.getLonLatFromPixel(evt.xy); + this.lastXy = evt.xy; + } + + var digits = parseInt(this.numdigits); + var newHtml = + this.prefix + + lonLat.lon.toFixed(digits) + + this.separator + + lonLat.lat.toFixed(digits) + + this.suffix; + + if (newHtml != this.element.innerHTML) { + this.element.innerHTML = newHtml; + } + }, + + /** + * + */ + setMap: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + this.map.events.register( 'mousemove', this, this.mousemove); + }, + + /** @final @type String */ + CLASS_NAME: "OpenLayers.Control.MousePosition" +}); diff --git a/theme/default/style.css b/theme/default/style.css index 1d9701a834..4daae6020e 100644 --- a/theme/default/style.css +++ b/theme/default/style.css @@ -18,3 +18,11 @@ left: 2px; bottom: 15px; } +div.olControlMousePosition { + bottom: 0em; + right: 3px; + display: block; + position: absolute; + font-family: Arial; + font-size: smaller; +}