From 15e5242611fdbdb4c93506cf4ee327e00e9e43af Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Tue, 16 Apr 2013 11:40:10 +0200 Subject: [PATCH] Add 'heading' property to ol.DeviceOrientation --- examples/device-orientation.html | 1 + examples/device-orientation.js | 1 + src/ol/deviceorientation.js | 23 ++++++++++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/examples/device-orientation.html b/examples/device-orientation.html index 1d00786255..dd1bb6215d 100644 --- a/examples/device-orientation.html +++ b/examples/device-orientation.html @@ -43,6 +43,7 @@

α :

β :

γ :

+

heading :

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 467d3dac9a..9b2fb247b4 100644 --- a/examples/device-orientation.js +++ b/examples/device-orientation.js @@ -29,4 +29,5 @@ deviceOrientation.on('changed', function() { document.getElementById('alpha').innerHTML = deviceOrientation.getAlpha(); document.getElementById('beta').innerHTML = deviceOrientation.getBeta(); document.getElementById('gamma').innerHTML = deviceOrientation.getGamma(); + document.getElementById('heading').innerHTML = deviceOrientation.getHeading(); }); diff --git a/src/ol/deviceorientation.js b/src/ol/deviceorientation.js index e9e8000880..4d27e163f4 100644 --- a/src/ol/deviceorientation.js +++ b/src/ol/deviceorientation.js @@ -1,4 +1,3 @@ -// FIXME: event.absolute goog.provide('ol.DeviceOrientation'); goog.provide('ol.DeviceOrientationProperty'); @@ -14,6 +13,7 @@ ol.DeviceOrientationProperty = { ALPHA: 'alpha', BETA: 'beta', GAMMA: 'gamma', + HEADING: 'heading', TRACKING: 'tracking' }; @@ -67,8 +67,12 @@ ol.DeviceOrientation.prototype.orientationChange_ = function(browserEvent) { var event = /** @type {DeviceOrientationEvent} */ (browserEvent.getBrowserEvent()); if (goog.isDefAndNotNull(event.alpha)) { - this.set(ol.DeviceOrientationProperty.ALPHA, - goog.math.toRadians(event.alpha)); + var alpha = goog.math.toRadians(event.alpha); + this.set(ol.DeviceOrientationProperty.ALPHA, alpha); + // event.absolute is undefined in iOS. + if (goog.isBoolean(event.absolute) && event.absolute) { + this.set(ol.DeviceOrientationProperty.HEADING, alpha); + } } if (goog.isDefAndNotNull(event.beta)) { this.set(ol.DeviceOrientationProperty.BETA, @@ -120,6 +124,19 @@ goog.exportProperty( ol.DeviceOrientation.prototype.getGamma); +/** + * @return {number|undefined} heading. + */ +ol.DeviceOrientation.prototype.getHeading = function() { + return /** @type {number} */ ( + this.get(ol.DeviceOrientationProperty.HEADING)); +}; +goog.exportProperty( + ol.DeviceOrientation.prototype, + 'getHeading', + ol.DeviceOrientation.prototype.getHeading); + + /** * @return {boolean|undefined} tracking. */