![](/img/trans.png)
[英]phonegap/cordova onDeviceReady() not firing android 4.1.2
[英]Phonegap in Android onDeviceReady function
我在android開發中使用phonegap。 我寫了PoC,但是我不知道為什么它不會改變profile變量的范圍。 其實
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);
});
};
很簡單,這是因為對navigator.geolocation.getCurrentPosition()的調用是異步調用。 這樣,程序將繼續執行,您將看到警報。 顯示警報后的某個時間,您的geoCode類的onSuccess調用稱為更新profile.latitude值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.