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="span12">
<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>
@@ -48,7 +43,23 @@
</div>
<div id="tags">mouse-position, openstreetmap</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>

View File

@@ -4,7 +4,9 @@ goog.require('ol.View2D');
goog.require('ol.control.MousePosition');
goog.require('ol.control.defaults');
goog.require('ol.coordinate');
goog.require('ol.dom.Input');
goog.require('ol.layer.TileLayer');
goog.require('ol.proj');
goog.require('ol.source.OSM');
var control = new ol.control.MousePosition({
@@ -32,6 +34,13 @@ var map = new ol.Map({
})
});
document.getElementById('projection').addEventListener('change', function() {
control.setProjection(this.value);
}, false);
var projection = new ol.dom.Input(document.getElementById('projection'));
projection.on('change:value', function() {
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
@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.style');
goog.require('ol.CoordinateFormatType');
goog.require('ol.Object');
goog.require('ol.Pixel');
goog.require('ol.Projection');
goog.require('ol.TransformFunction');
@@ -17,6 +18,15 @@ goog.require('ol.control.Control');
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
@@ -53,17 +63,16 @@ ol.control.MousePosition = function(opt_options) {
target: options.target
});
/**
* @private
* @type {ol.Projection}
*/
this.projection_ = ol.proj.get(options.projection);
goog.events.listen(this,
ol.Object.getChangeEventType(ol.control.MousePositionProperty.PROJECTION),
this.handleProjectionChanged_, false, this);
/**
* @private
* @type {ol.CoordinateFormatType|undefined}
*/
this.coordinateFormat_ = options.coordinateFormat;
if (goog.isDef(options.coordinateFormat)) {
this.setCoordinateFormat(options.coordinateFormat);
}
if (goog.isDef(options.projection)) {
this.setProjection(ol.proj.get(options.projection));
}
/**
* @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() {
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) {
this.projection_ = ol.proj.get(projection);
this.transform_ = null;
this.set(ol.control.MousePositionProperty.PROJECTION, projection);
};
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_;
if (!goog.isNull(pixel)) {
if (goog.isNull(this.transform_)) {
if (!goog.isNull(this.projection_)) {
var projection = this.getProjection();
if (goog.isDef(projection)) {
this.transform_ = ol.proj.getTransformFromProjections(
this.mapProjection_, this.projection_);
this.mapProjection_, projection);
} else {
this.transform_ = ol.proj.identityTransform;
}
@@ -193,8 +244,9 @@ ol.control.MousePosition.prototype.updateHTML_ = function(pixel) {
var coordinate = map.getCoordinateFromPixel(pixel);
if (!goog.isNull(coordinate)) {
this.transform_(coordinate, coordinate);
if (goog.isDef(this.coordinateFormat_)) {
html = this.coordinateFormat_(coordinate);
var coordinateFormat = this.getCoordinateFormat();
if (goog.isDef(coordinateFormat)) {
html = coordinateFormat(coordinate);
} else {
html = coordinate.toString();
}