javascript - Android onDeviceReady 函数中的 Phonegap

标签 javascript android cordova

我在 android 开发中使用 phonegap。我写了那个 PoC,但是我不明白为什么它不改变配置文件变量的纬度。其实

alert(profile.latitude);

之前运行
geoCode.setLocation();

这是我的代码;

document.addEventListener("deviceready", onDeviceReady, false);

var profile = {
    name: "",
    id: "red",
    latitude: "xx",
    longtitude: "",
    setCoordinates: function (latit, longt) {
        this.latitude = latit;
        this.longtitude = longt;
    }
};

var geoCode = {
    onSuccess: function (position) {
        profile.latitude = position.coords.latitude;
    },

    onError: function (error) {
    },

    setLocation : function () {
        navigator.geolocation.getCurrentPosition(this.onSuccess, this.onError);
    }
};

// Wait for PhoneGap to load
//

function onDeviceReady() {

    geoCode.setLocation();
    //alert("2");
    alert(profile.latitude);
};

提前致谢

最佳答案

navigator.geolocation.getCurrentPosition 是一个异步函数。你需要做类似的事情:

var geoCode = {


setLocation : function (callback) {

    onSuccess: function (position) {
       callback(position.coords.latitude);
    },

    onError: function (error) {
    },
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

};

// Wait for PhoneGap to load
//

function onDeviceReady() {

    geoCode.setLocation(function(latitude) {
        alert(latitude);
    });
};

关于javascript - Android onDeviceReady 函数中的 Phonegap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255367/

相关文章:

javascript - 如何检查循环中的所有内容是否为真?

android - Aapt添加图书馆资源

Android AVD-修改API级别,改变APP View

java - android :onClick and View. OnClickListener 之间的关系

JavaScript 正则表达式 : get all numbers having between 2 and 6 digits but not 5

javascript - 控制 Javascript 选择器

android - Cordova Android构建版本代码与Gradle

javascript - Ajax/ header 不匹配?

ios - 如何在phonegap中将URL列入白名单?

javascript - 获取JSON IP + 国家代码