Question

I have a question about using a value I receive from the device orientation event in another function. This is what I have :

$(window).ready(function(){ 
    $("#btnShowDirection").click(showDirection);

    // Device orientation
    if (window.DeviceOrientationEvent) {
        window.addEventListener("deviceorientation", handleOrientation, false);
    } else {
        alert("Device Orientation is not available");
    }
}); // END OF DOCUMENT READY

// orientation object to save heading of the device
var orientation = {};

function handleOrientation(orientData) {
    var alpha = orientData.alpha;
    orientation.value = alpha;
}

var watchProcess = null;  
function showDirection() {
    if (navigator.geolocation) {
        if (watchProcess == null) {
            navigator.geolocation.watchPosition(geoWatchSucces,geoError);  
        }
    } else {
        alert("Geolocation is not supported!");
    }
}

function geoWatchSucces(position) {
    alert(orientation.value);
}

The alert(orientation.value); gives back undefined. How can I fix this? I want to use that variable in my watchprocess success function.

jsfiddle: http://jsfiddle.net/HJTNJ/

Niels

Was it helpful?

Solution

The orientation.value is set in the $(window).ready scope, that way in your geoWatchSucces function: orientation is just an empty object...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top