Javascript Android onDeviceReady函数中的Phonegap

Javascript Android onDeviceReady函数中的Phonegap,javascript,android,cordova,Javascript,Android,Cordova,我在android开发中使用phonegap。我写了PoC,但是我不明白为什么它不改变纵断面变量的纬度。实际上 alert(profile.latitude); 在 geoCode.setLocation(); 这是我的密码 document.addEventListener("deviceready", onDeviceReady, false); var profile = { name: "", id: "red", latitude: "xx", l

我在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()的调用是一个异步调用。因此,程序将继续执行,您将看到警报。显示警报后的某个时间,将调用地理代码类的onSuccess调用来更新profile.latitude值

很简单,这是因为对navigator.geolocation.getCurrentPosition()的调用是一个异步调用。因此,程序将继续执行,您将看到警报。显示警报后的某个时间,将调用地理代码类的onSuccess调用来更新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);
    });
};

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