我在 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/