Handle projection change in ol.Geolocation

This commit is contained in:
Frederic Junod
2013-02-04 09:46:31 +01:00
parent 38f245c5c8
commit 6675de437f
+29 -7
View File
@@ -3,6 +3,7 @@
goog.provide('ol.Geolocation'); goog.provide('ol.Geolocation');
goog.provide('ol.GeolocationProperty'); goog.provide('ol.GeolocationProperty');
goog.require('goog.functions');
goog.require('ol.Coordinate'); goog.require('ol.Coordinate');
goog.require('ol.Object'); goog.require('ol.Object');
goog.require('ol.Projection'); goog.require('ol.Projection');
@@ -28,8 +29,16 @@ ol.Geolocation = function(opt_positionOptions) {
goog.base(this); goog.base(this);
// set the default projection /**
this.setProjection(ol.Projection.getFromCode('EPSG:4326')); * The unprojected (EPSG:4326) device position.
* @private
* @type {ol.Coordinate}
*/
this.position_ = null;
goog.events.listen(
this, ol.Object.getChangedEventType(ol.GeolocationProperty.PROJECTION),
this.handleProjectionChanged, false, this);
/** /**
* @private * @private
@@ -52,14 +61,28 @@ ol.Geolocation.prototype.disposeInternal = function() {
}; };
/**
* @protected
*/
ol.Geolocation.prototype.handleProjectionChanged = function() {
this.transformCoords_ = ol.Projection.getTransform(
ol.Projection.getFromCode('EPSG:4326'), this.getProjection());
if (!goog.isNull(this.position_)) {
this.set(ol.GeolocationProperty.POSITION,
this.transformCoords_(this.position_));
}
};
/** /**
* @private * @private
* @param {GeolocationPosition} position position event. * @param {GeolocationPosition} position position event.
*/ */
ol.Geolocation.prototype.positionChange_ = function(position) { ol.Geolocation.prototype.positionChange_ = function(position) {
var coords = position.coords; var coords = position.coords;
var coord = new ol.Coordinate(coords.longitude, coords.latitude); this.position_ = new ol.Coordinate(coords.longitude, coords.latitude);
this.set(ol.GeolocationProperty.POSITION, this.transformCoords_(coord)); this.set(ol.GeolocationProperty.POSITION,
this.transformCoords_(this.position_));
this.set(ol.GeolocationProperty.ACCURACY, coords.accuracy); this.set(ol.GeolocationProperty.ACCURACY, coords.accuracy);
}; };
@@ -118,9 +141,6 @@ goog.exportProperty(
*/ */
ol.Geolocation.prototype.setProjection = function(projection) { ol.Geolocation.prototype.setProjection = function(projection) {
this.set(ol.GeolocationProperty.PROJECTION, projection); this.set(ol.GeolocationProperty.PROJECTION, projection);
this.transformCoords_ = ol.Projection.getTransform(
ol.Projection.getFromCode('EPSG:4326'), projection);
}; };
goog.exportProperty( goog.exportProperty(
ol.Geolocation.prototype, ol.Geolocation.prototype,
@@ -130,5 +150,7 @@ goog.exportProperty(
/** /**
* @private * @private
* @param {ol.Coordinate} coordinate Coordinate.
* @return {ol.Coordinate} Coordinate.
*/ */
ol.Geolocation.prototype.transformCoords_ = goog.functions.identity; ol.Geolocation.prototype.transformCoords_ = goog.functions.identity;