Add 'heading' property to ol.DeviceOrientation

This commit is contained in:
Frederic Junod
2013-04-16 11:40:10 +02:00
parent c431cc7f63
commit 15e5242611
3 changed files with 22 additions and 3 deletions

View File

@@ -43,6 +43,7 @@
<p>&alpha; : <code id="alpha"></code></p>
<p>&beta; : <code id="beta"></code></p>
<p>&gamma; : <code id="gamma"></code></p>
<p>heading : <code id="heading"></code></p>
<p id="shortdesc">Listen to DeviceOrientation events</p>
<div id="docs">
<p>See the <a href="device-orientation.js" target="_blank">device-orientation.js source</a> to see how this is done.</p>

View File

@@ -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();
});

View File

@@ -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.
*/