From 85ca630935b93d4014f478cbd6d20d8f9d8a2458 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 13 Aug 2012 16:41:31 +0200 Subject: [PATCH] Add ol.control.MousePosition --- src/ol/control/mouseposition.js | 124 ++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/ol/control/mouseposition.js diff --git a/src/ol/control/mouseposition.js b/src/ol/control/mouseposition.js new file mode 100644 index 0000000000..d03bb38646 --- /dev/null +++ b/src/ol/control/mouseposition.js @@ -0,0 +1,124 @@ +goog.provide('ol.control.MousePosition'); + +goog.require('goog.events'); +goog.require('goog.events.EventType'); +goog.require('ol.Control'); +goog.require('ol.MapProperty'); +goog.require('ol.Object'); +goog.require('ol.Projection'); +goog.require('ol.TransformFunction'); + + + +/** + * @constructor + * @extends {ol.Control} + * @param {ol.Map} map Map. + * @param {ol.Projection=} opt_projection Projection. + * @param {ol.CoordinateFormatType=} opt_coordinateFormat Coordinate format. + * @param {string=} opt_undefinedHTML Undefined HTML. + */ +ol.control.MousePosition = + function(map, opt_projection, opt_coordinateFormat, opt_undefinedHTML) { + + goog.base(this, map); + + /** + * @private + * @type {Element} + */ + this.divElement_ = goog.dom.createElement(goog.dom.TagName.DIV); + + /** + * @private + * @type {ol.Projection} + */ + this.projection_ = opt_projection || null; + + /** + * @private + * @type {ol.CoordinateFormatType|undefined} + */ + this.coordinateFormat_ = opt_coordinateFormat; + + /** + * @private + * @type {string} + */ + this.undefinedHTML_ = opt_undefinedHTML || ''; + + goog.events.listen(map, + ol.Object.getChangedEventType(ol.MapProperty.PROJECTION), + this.handleMapProjectionChanged, false, this); + + goog.events.listen(map.getTarget(), goog.events.EventType.MOUSEMOVE, + this.handleMouseMove, false, this); + + goog.events.listen(map.getTarget(), goog.events.EventType.MOUSEOUT, + this.handleMouseOut, false, this); + + this.handleMapProjectionChanged(); + +}; +goog.inherits(ol.control.MousePosition, ol.Control); + + +/** + * @private + * @type {ol.TransformFunction} + */ +ol.control.MousePosition.prototype.transform_; + + +/** + * @inheritDoc + */ +ol.control.MousePosition.prototype.getElement = function() { + return this.divElement_; +}; + + +/** + * @protected + */ +ol.control.MousePosition.prototype.handleMapProjectionChanged = function() { + var map = this.getMap(); + var mapProjection = map.getProjection(); + if (!goog.isDef(mapProjection) || goog.isNull(this.projection_)) { + this.transform_ = ol.Projection.identityTransform; + } else { + this.transform_ = + ol.Projection.getTransform(mapProjection, this.projection_); + } + this.divElement_.innerHTML = this.undefinedHTML_; +}; + + +/** + * @param {goog.events.BrowserEvent} browserEvent Browser event. + * @protected + */ +ol.control.MousePosition.prototype.handleMouseOut = function(browserEvent) { + this.divElement_.innerHTML = this.undefinedHTML_; +}; + + +/** + * @param {goog.events.BrowserEvent} browserEvent Browser event. + * @protected + */ +ol.control.MousePosition.prototype.handleMouseMove = function(browserEvent) { + var map = this.getMap(); + var pixel = new ol.Pixel(browserEvent.offsetX, browserEvent.offsetY); + var coordinate = map.getCoordinateFromPixel(pixel); + if (goog.isDef(coordinate)) { + coordinate = this.transform_(coordinate); + } + var html; + if (goog.isDef(this.coordinateFormat_)) { + html = this.coordinateFormat_(coordinate); + } else { + html = coordinate.toString(); + } + this.divElement_.innerHTML = html; +};