Merge pull request #306 from fredj/geolocation

Activate / deactivate geolocation
This commit is contained in:
Frédéric Junod
2013-03-11 04:04:04 -07:00
+87 -17
View File
@@ -22,7 +22,9 @@ ol.GeolocationProperty = {
HEADING: 'heading', HEADING: 'heading',
POSITION: 'position', POSITION: 'position',
PROJECTION: 'projection', PROJECTION: 'projection',
SPEED: 'speed' SPEED: 'speed',
TRACKING: 'tracking',
TRACKING_OPTIONS: 'trackingOptions'
}; };
@@ -30,9 +32,8 @@ ol.GeolocationProperty = {
/** /**
* @constructor * @constructor
* @extends {ol.Object} * @extends {ol.Object}
* @param {GeolocationPositionOptions=} opt_positionOptions PositionOptions.
*/ */
ol.Geolocation = function(opt_positionOptions) { ol.Geolocation = function() {
goog.base(this); goog.base(this);
@@ -43,20 +44,20 @@ ol.Geolocation = function(opt_positionOptions) {
*/ */
this.position_ = null; this.position_ = null;
if (ol.Geolocation.SUPPORTED) { /**
goog.events.listen( * @private
this, ol.Object.getChangedEventType(ol.GeolocationProperty.PROJECTION), * @type {number|undefined}
this.handleProjectionChanged_, false, this); */
this.watchId_;
/** this.setTracking(false);
* @private
* @type {number} goog.events.listen(
*/ this, ol.Object.getChangedEventType(ol.GeolocationProperty.PROJECTION),
this.watchId_ = navigator.geolocation.watchPosition( this.handleProjectionChanged_, false, this);
goog.bind(this.positionChange_, this), goog.events.listen(
goog.bind(this.positionError_, this), this, ol.Object.getChangedEventType(ol.GeolocationProperty.TRACKING),
opt_positionOptions); this.handleTrackingChanged_, false, this);
}
}; };
goog.inherits(ol.Geolocation, ol.Object); goog.inherits(ol.Geolocation, ol.Object);
@@ -65,7 +66,7 @@ goog.inherits(ol.Geolocation, ol.Object);
* @inheritDoc * @inheritDoc
*/ */
ol.Geolocation.prototype.disposeInternal = function() { ol.Geolocation.prototype.disposeInternal = function() {
navigator.geolocation.clearWatch(this.watchId_); this.setTracking(false);
goog.base(this, 'disposeInternal'); goog.base(this, 'disposeInternal');
}; };
@@ -88,6 +89,25 @@ ol.Geolocation.prototype.handleProjectionChanged_ = function() {
}; };
/**
* @private
*/
ol.Geolocation.prototype.handleTrackingChanged_ = function() {
if (ol.Geolocation.SUPPORTED) {
var tracking = this.getTracking();
if (tracking && !goog.isDef(this.watchId_)) {
this.watchId_ = navigator.geolocation.watchPosition(
goog.bind(this.positionChange_, this),
goog.bind(this.positionError_, this),
this.getTrackingOptions());
} else if (!tracking && goog.isDef(this.watchId_)) {
navigator.geolocation.clearWatch(this.watchId_);
this.watchId_ = undefined;
}
}
};
/** /**
* @const * @const
* @type {boolean} Is supported. * @type {boolean} Is supported.
@@ -220,6 +240,32 @@ goog.exportProperty(
ol.Geolocation.prototype.getSpeed); ol.Geolocation.prototype.getSpeed);
/**
* @return {boolean|undefined} tracking.
*/
ol.Geolocation.prototype.getTracking = function() {
return /** @type {boolean} */ (
this.get(ol.GeolocationProperty.TRACKING));
};
goog.exportProperty(
ol.Geolocation.prototype,
'getTracking',
ol.Geolocation.prototype.getTracking);
/**
* @return {GeolocationPositionOptions|undefined} tracking options.
*/
ol.Geolocation.prototype.getTrackingOptions = function() {
return /** @type {GeolocationPositionOptions} */ (
this.get(ol.GeolocationProperty.TRACKING_OPTIONS));
};
goog.exportProperty(
ol.Geolocation.prototype,
'getTrackingOptions',
ol.Geolocation.prototype.getTrackingOptions);
/** /**
* @param {ol.Projection} projection Projection. * @param {ol.Projection} projection Projection.
*/ */
@@ -232,6 +278,30 @@ goog.exportProperty(
ol.Geolocation.prototype.setProjection); ol.Geolocation.prototype.setProjection);
/**
* @param {boolean} tracking Enable or disable tracking.
*/
ol.Geolocation.prototype.setTracking = function(tracking) {
this.set(ol.GeolocationProperty.TRACKING, tracking);
};
goog.exportProperty(
ol.Geolocation.prototype,
'setTracking',
ol.Geolocation.prototype.setTracking);
/**
* @param {GeolocationPositionOptions} options Tracking options.
*/
ol.Geolocation.prototype.setTrackingOptions = function(options) {
this.set(ol.GeolocationProperty.TRACKING_OPTIONS, options);
};
goog.exportProperty(
ol.Geolocation.prototype,
'setTrackingOptions',
ol.Geolocation.prototype.setTrackingOptions);
/** /**
* @private * @private
* @param {Array.<number>} input Input coordinate values. * @param {Array.<number>} input Input coordinate values.