在其他功能中使用来自设备方向的值

Use value from device orientation in other function

本文关键字:方向 功能 其他      更新时间:2023-09-26

我有一个关于在另一个函数中使用从设备方向事件接收的值的问题。这是我的文件:

$(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);返回undefined。我该如何解决这个问题?我想在我的手表进程成功函数中使用这个变量。

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

尼尔斯

方向。值在$(window)中设置。