Add 'projection' and 'coordinateFormat' to ol.control.MousePosition

This commit is contained in:
Frederic Junod
2013-06-20 11:56:49 +02:00
parent 9d63217778
commit 22adf354e2
4 changed files with 99 additions and 29 deletions

View File

@@ -30,11 +30,6 @@
<div class="row-fluid"> <div class="row-fluid">
<div class="span12"> <div class="span12">
<div id="map" class="map"></div> <div id="map" class="map"></div>
<select id="projection">
<option value="EPSG:4326">EPSG:4326</option>
<option value="EPSG:3857">EPSG:3857</option>
</select>
<div style="display: inline-block;" id="mouse-position"></div>
</div> </div>
</div> </div>
@@ -48,7 +43,23 @@
</div> </div>
<div id="tags">mouse-position, openstreetmap</div> <div id="tags">mouse-position, openstreetmap</div>
</div> </div>
<div class="span8 pull-right">
<div id="mouse-position">&nbsp;</div>
</div>
</div>
<div class="row-fluid">
<div class="span8">
<form>
<label>Projection </label>
<select id="projection">
<option value="EPSG:4326">EPSG:4326</option>
<option value="EPSG:3857">EPSG:3857</option>
</select>
<label>Precision </label>
<input id="precision" type="number" min="0" max="12" value="4"/>
</form>
</div>
</div> </div>
</div> </div>

View File

@@ -4,7 +4,9 @@ goog.require('ol.View2D');
goog.require('ol.control.MousePosition'); goog.require('ol.control.MousePosition');
goog.require('ol.control.defaults'); goog.require('ol.control.defaults');
goog.require('ol.coordinate'); goog.require('ol.coordinate');
goog.require('ol.dom.Input');
goog.require('ol.layer.TileLayer'); goog.require('ol.layer.TileLayer');
goog.require('ol.proj');
goog.require('ol.source.OSM'); goog.require('ol.source.OSM');
var control = new ol.control.MousePosition({ var control = new ol.control.MousePosition({
@@ -32,6 +34,13 @@ var map = new ol.Map({
}) })
}); });
document.getElementById('projection').addEventListener('change', function() { var projection = new ol.dom.Input(document.getElementById('projection'));
control.setProjection(this.value); projection.on('change:value', function() {
}, false); control.setProjection(ol.proj.get(projection.getValue()));
});
var precision = new ol.dom.Input(document.getElementById('precision'));
precision.on('change:value', function() {
var format = ol.coordinate.createStringXY(precision.getValue());
control.setCoordinateFormat(format);
});

View File

@@ -1,4 +1,2 @@
@exportClass ol.control.MousePosition ol.control.MousePositionOptions @exportClass ol.control.MousePosition ol.control.MousePositionOptions
@exportProperty ol.control.MousePosition.prototype.setMap @exportProperty ol.control.MousePosition.prototype.setMap
@exportProperty ol.control.MousePosition.prototype.getProjection
@exportProperty ol.control.MousePosition.prototype.setProjection

View File

@@ -10,6 +10,7 @@ goog.require('goog.events');
goog.require('goog.events.EventType'); goog.require('goog.events.EventType');
goog.require('goog.style'); goog.require('goog.style');
goog.require('ol.CoordinateFormatType'); goog.require('ol.CoordinateFormatType');
goog.require('ol.Object');
goog.require('ol.Pixel'); goog.require('ol.Pixel');
goog.require('ol.Projection'); goog.require('ol.Projection');
goog.require('ol.TransformFunction'); goog.require('ol.TransformFunction');
@@ -17,6 +18,15 @@ goog.require('ol.control.Control');
goog.require('ol.proj'); goog.require('ol.proj');
/**
* @enum {string}
*/
ol.control.MousePositionProperty = {
PROJECTION: 'projection',
COORDINATE_FORMAT: 'coordinateFormat'
};
/** /**
* Create a new control to show the position of the mouse in the map's * Create a new control to show the position of the mouse in the map's
@@ -53,17 +63,16 @@ ol.control.MousePosition = function(opt_options) {
target: options.target target: options.target
}); });
/** goog.events.listen(this,
* @private ol.Object.getChangeEventType(ol.control.MousePositionProperty.PROJECTION),
* @type {ol.Projection} this.handleProjectionChanged_, false, this);
*/
this.projection_ = ol.proj.get(options.projection);
/** if (goog.isDef(options.coordinateFormat)) {
* @private this.setCoordinateFormat(options.coordinateFormat);
* @type {ol.CoordinateFormatType|undefined} }
*/ if (goog.isDef(options.projection)) {
this.coordinateFormat_ = options.coordinateFormat; this.setProjection(ol.proj.get(options.projection));
}
/** /**
* @private * @private
@@ -118,11 +127,37 @@ ol.control.MousePosition.prototype.handleMapPostrender = function(mapEvent) {
/** /**
* @return {ol.Projection} projection. * @private
*/
ol.control.MousePosition.prototype.handleProjectionChanged_ = function() {
this.transform_ = null;
};
/**
* @return {ol.CoordinateFormatType|undefined} projection.
*/
ol.control.MousePosition.prototype.getCoordinateFormat = function() {
return /** @type {ol.CoordinateFormatType|undefined} */ (
this.get(ol.control.MousePositionProperty.COORDINATE_FORMAT));
};
goog.exportProperty(
ol.control.MousePosition.prototype,
'getCoordinateFormat',
ol.control.MousePosition.prototype.getCoordinateFormat);
/**
* @return {ol.Projection|undefined} projection.
*/ */
ol.control.MousePosition.prototype.getProjection = function() { ol.control.MousePosition.prototype.getProjection = function() {
return this.projection_; return /** @type {ol.Projection|undefined} */ (
this.get(ol.control.MousePositionProperty.PROJECTION));
}; };
goog.exportProperty(
ol.control.MousePosition.prototype,
'getProjection',
ol.control.MousePosition.prototype.getProjection);
/** /**
@@ -166,12 +201,27 @@ ol.control.MousePosition.prototype.setMap = function(map) {
/** /**
* @param {ol.ProjectionLike} projection Projection. * @param {ol.CoordinateFormatType} format Coordinate format.
*/
ol.control.MousePosition.prototype.setCoordinateFormat = function(format) {
this.set(ol.control.MousePositionProperty.COORDINATE_FORMAT, format);
};
goog.exportProperty(
ol.control.MousePosition.prototype,
'setCoordinateFormat',
ol.control.MousePosition.prototype.setCoordinateFormat);
/**
* @param {ol.Projection} projection Projection.
*/ */
ol.control.MousePosition.prototype.setProjection = function(projection) { ol.control.MousePosition.prototype.setProjection = function(projection) {
this.projection_ = ol.proj.get(projection); this.set(ol.control.MousePositionProperty.PROJECTION, projection);
this.transform_ = null;
}; };
goog.exportProperty(
ol.control.MousePosition.prototype,
'setProjection',
ol.control.MousePosition.prototype.setProjection);
/** /**
@@ -182,9 +232,10 @@ ol.control.MousePosition.prototype.updateHTML_ = function(pixel) {
var html = this.undefinedHTML_; var html = this.undefinedHTML_;
if (!goog.isNull(pixel)) { if (!goog.isNull(pixel)) {
if (goog.isNull(this.transform_)) { if (goog.isNull(this.transform_)) {
if (!goog.isNull(this.projection_)) { var projection = this.getProjection();
if (goog.isDef(projection)) {
this.transform_ = ol.proj.getTransformFromProjections( this.transform_ = ol.proj.getTransformFromProjections(
this.mapProjection_, this.projection_); this.mapProjection_, projection);
} else { } else {
this.transform_ = ol.proj.identityTransform; this.transform_ = ol.proj.identityTransform;
} }
@@ -193,8 +244,9 @@ ol.control.MousePosition.prototype.updateHTML_ = function(pixel) {
var coordinate = map.getCoordinateFromPixel(pixel); var coordinate = map.getCoordinateFromPixel(pixel);
if (!goog.isNull(coordinate)) { if (!goog.isNull(coordinate)) {
this.transform_(coordinate, coordinate); this.transform_(coordinate, coordinate);
if (goog.isDef(this.coordinateFormat_)) { var coordinateFormat = this.getCoordinateFormat();
html = this.coordinateFormat_(coordinate); if (goog.isDef(coordinateFormat)) {
html = coordinateFormat(coordinate);
} else { } else {
html = coordinate.toString(); html = coordinate.toString();
} }