From c431cc7f63c83b632cd11cb4ec01c7bd73966ac9 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 16 Apr 2013 09:40:59 +0200 Subject: [PATCH] Add 'tracking' property to ol.DeviceOrientation --- examples/device-orientation.html | 9 ++-- examples/device-orientation.js | 3 ++ src/ol/deviceorientation.js | 70 +++++++++++++++++++++++++++++--- 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/examples/device-orientation.html b/examples/device-orientation.html index 0423c68dc2..1d00786255 100644 --- a/examples/device-orientation.html +++ b/examples/device-orientation.html @@ -37,9 +37,12 @@

Device orientation example

-

α : rad

-

β : rad

-

γ : rad

+ +

α :

+

β :

+

γ :

Listen to DeviceOrientation events

See the device-orientation.js source to see how this is done.

diff --git a/examples/device-orientation.js b/examples/device-orientation.js index 42f4836260..467d3dac9a 100644 --- a/examples/device-orientation.js +++ b/examples/device-orientation.js @@ -2,6 +2,7 @@ goog.require('ol.DeviceOrientation'); goog.require('ol.Map'); goog.require('ol.RendererHints'); goog.require('ol.View2D'); +goog.require('ol.dom.Input'); goog.require('ol.layer.TileLayer'); goog.require('ol.source.OSM'); @@ -21,6 +22,8 @@ var map = new ol.Map({ }); var deviceOrientation = new ol.DeviceOrientation(); +var track = new ol.dom.Input(document.getElementById('track')); +track.bindTo('checked', deviceOrientation, 'tracking'); deviceOrientation.on('changed', function() { document.getElementById('alpha').innerHTML = deviceOrientation.getAlpha(); diff --git a/src/ol/deviceorientation.js b/src/ol/deviceorientation.js index 24308fedff..e9e8000880 100644 --- a/src/ol/deviceorientation.js +++ b/src/ol/deviceorientation.js @@ -1,4 +1,3 @@ -// FIXME: tracking property (same as ol.Geolocation) // FIXME: event.absolute goog.provide('ol.DeviceOrientation'); goog.provide('ol.DeviceOrientationProperty'); @@ -14,7 +13,8 @@ goog.require('ol.Object'); ol.DeviceOrientationProperty = { ALPHA: 'alpha', BETA: 'beta', - GAMMA: 'gamma' + GAMMA: 'gamma', + TRACKING: 'tracking' }; @@ -27,14 +27,30 @@ ol.DeviceOrientation = function() { goog.base(this); - if (ol.DeviceOrientation.SUPPORTED) { - goog.events.listen(window, 'deviceorientation', - this.orientationChange_, false, this); - } + /** + * @private + * @type {?number} + */ + this.listenerKey_ = null; + + this.setTracking(false); + + goog.events.listen(this, + ol.Object.getChangedEventType(ol.DeviceOrientationProperty.TRACKING), + this.handleTrackingChanged_, false, this); }; goog.inherits(ol.DeviceOrientation, ol.Object); +/** + * @inheritDoc + */ +ol.DeviceOrientation.prototype.disposeInternal = function() { + this.setTracking(false); + goog.base(this, 'disposeInternal'); +}; + + /** * Is supported. * @const @@ -102,3 +118,45 @@ goog.exportProperty( ol.DeviceOrientation.prototype, 'getGamma', ol.DeviceOrientation.prototype.getGamma); + + +/** + * @return {boolean|undefined} tracking. + */ +ol.DeviceOrientation.prototype.getTracking = function() { + return /** @type {boolean} */ ( + this.get(ol.DeviceOrientationProperty.TRACKING)); +}; +goog.exportProperty( + ol.DeviceOrientation.prototype, + 'getTracking', + ol.DeviceOrientation.prototype.getTracking); + + +/** + * @private + */ +ol.DeviceOrientation.prototype.handleTrackingChanged_ = function() { + if (ol.DeviceOrientation.SUPPORTED) { + var tracking = this.getTracking(); + if (tracking && goog.isNull(this.listenerKey_)) { + this.listenerKey_ = goog.events.listen(window, 'deviceorientation', + this.orientationChange_, false, this); + } else if (!tracking && !goog.isNull(this.listenerKey_)) { + goog.events.unlistenByKey(this.listenerKey_); + this.listenerKey_ = null; + } + } +}; + + +/** + * @param {boolean} tracking Enable or disable tracking. + */ +ol.DeviceOrientation.prototype.setTracking = function(tracking) { + this.set(ol.DeviceOrientationProperty.TRACKING, tracking); +}; +goog.exportProperty( + ol.DeviceOrientation.prototype, + 'setTracking', + ol.DeviceOrientation.prototype.setTracking);