From 916dde00452339608f5e53eff442659480c9711d Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Mon, 6 May 2013 11:46:48 +0200 Subject: [PATCH] Add ol.control.MousePosition.setProjection --- examples/mouse-position.html | 6 ++++- examples/mouse-position.js | 25 ++++++++++--------- src/ol/control/mousepositioncontrol.exports | 1 + src/ol/control/mousepositioncontrol.js | 27 ++++++++++++--------- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/examples/mouse-position.html b/examples/mouse-position.html index 74f85c76f1..620b8e5544 100644 --- a/examples/mouse-position.html +++ b/examples/mouse-position.html @@ -30,7 +30,11 @@
- + +
diff --git a/examples/mouse-position.js b/examples/mouse-position.js index 36d6615ec4..848b002d4b 100644 --- a/examples/mouse-position.js +++ b/examples/mouse-position.js @@ -7,19 +7,18 @@ goog.require('ol.coordinate'); goog.require('ol.layer.TileLayer'); goog.require('ol.source.OSM'); +var control = new ol.control.MousePosition({ + coordinateFormat: ol.coordinate.createStringXY(4), + projection: 'EPSG:4326', + // comment the following two lines to have the mouse position + // be placed within the map. + className: 'custom-mouse-position', + target: document.getElementById('mouse-position'), + undefinedHTML: ' ' +}); var map = new ol.Map({ - controls: ol.control.defaults({}, [ - new ol.control.MousePosition({ - coordinateFormat: ol.coordinate.toStringHDMS, - projection: 'EPSG:4326', - // comment the following two lines to have the mouse position - // be placed within the map. - className: 'custom-mouse-position', - target: document.getElementById('mouse-position'), - undefinedHTML: ' ' - }) - ]), + controls: ol.control.defaults({}, [control]), layers: [ new ol.layer.TileLayer({ source: new ol.source.OSM() @@ -32,3 +31,7 @@ var map = new ol.Map({ zoom: 2 }) }); + +document.getElementById('projection').addEventListener('change', function() { + control.setProjection(this.value); +}, false); diff --git a/src/ol/control/mousepositioncontrol.exports b/src/ol/control/mousepositioncontrol.exports index 925d6c0720..7d82e5f436 100644 --- a/src/ol/control/mousepositioncontrol.exports +++ b/src/ol/control/mousepositioncontrol.exports @@ -1,2 +1,3 @@ @exportClass ol.control.MousePosition ol.control.MousePositionOptions @exportProperty ol.control.MousePosition.prototype.setMap +@exportProperty ol.control.MousePosition.prototype.setProjection diff --git a/src/ol/control/mousepositioncontrol.js b/src/ol/control/mousepositioncontrol.js index 0e1d220b2c..5e1b369255 100644 --- a/src/ol/control/mousepositioncontrol.js +++ b/src/ol/control/mousepositioncontrol.js @@ -86,15 +86,9 @@ ol.control.MousePosition = function(opt_options) { /** * @private - * @type {ol.TransformFunction} + * @type {?ol.TransformFunction} */ - this.transform_ = ol.proj.identityTransform; - - /** - * @private - * @type {ol.Projection} - */ - this.renderedProjection_ = null; + this.transform_ = null; /** * @private @@ -114,7 +108,10 @@ ol.control.MousePosition.prototype.handleMapPostrender = function(mapEvent) { if (goog.isNull(frameState)) { this.mapProjection_ = null; } else { - this.mapProjection_ = frameState.view2DState.projection; + if (this.mapProjection_ != frameState.view2DState.projection) { + this.mapProjection_ = frameState.view2DState.projection; + this.transform_ = null; + } } this.updateHTML_(this.lastMouseMovePixel_); }; @@ -160,6 +157,15 @@ ol.control.MousePosition.prototype.setMap = function(map) { }; +/** + * @param {ol.ProjectionLike} projection Projection. + */ +ol.control.MousePosition.prototype.setProjection = function(projection) { + this.projection_ = ol.proj.get(projection); + this.transform_ = null; +}; + + /** * @param {?ol.Pixel} pixel Pixel. * @private @@ -167,14 +173,13 @@ ol.control.MousePosition.prototype.setMap = function(map) { ol.control.MousePosition.prototype.updateHTML_ = function(pixel) { var html = this.undefinedHTML_; if (!goog.isNull(pixel)) { - if (this.renderedProjection_ != this.mapProjection_) { + if (goog.isNull(this.transform_)) { if (!goog.isNull(this.projection_)) { this.transform_ = ol.proj.getTransformFromProjections( this.mapProjection_, this.projection_); } else { this.transform_ = ol.proj.identityTransform; } - this.renderedProjection_ = this.mapProjection_; } var map = this.getMap(); var coordinate = map.getCoordinateFromPixel(pixel);