2013-05-16 30 views
0

我有一個關於在另一個函數中使用從設備方向事件接收到的值的問題。 這是我有:在其他函數中使用來自設備方向的值

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

alert(orientation.value);還給不確定。 我該如何解決這個問題?我想在我的watchprocess成功函數中使用該變量。

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

尼爾斯

回答

1

的orientation.value在$(窗口)。就緒範圍,你geoWatchSucces功能設置方式:定位只是一個空對象...

+0

謝謝!你是我的英雄! – nielsv

+0

不客氣! – Ferdau

相關問題