如何在调用navigator.geolocation.getcurrentPosition
时传入一个或多个参数来成功回调?
如何将 deviceready
从 foundLoc
传递给 getGeoLoc
方法?
var app = {
onDeviceReady: function () {
alert = window.alert || navigator.notification.alert;
app.getGeoLoc('deviceready');
},
getGeoLoc: function (id) {
navigator.geolocation.getCurrentPosition(this.foundLoc, this.noLoc, { timeout: 3 });
},
foundLoc: function (position) {
var parentElement = document.getElementById('deviceready');
var lat = parentElement.querySelector('#lat');
var long = parentElement.querySelector('#long');
lat.innerHTML = position.coords.latitude;
long.innerHTML = position.coords.longitude;
},
noLoc: function () {
alert('device has no GPS or access is denied.');
}
};
最佳答案
将地理位置回调包装在 function(position) {} 中,如下所示。然后,您可以向实际的回调函数添加任意数量的参数。
var app = {
getGeoLoc : function (id) {
var self = this;
navigator.geolocation.getCurrentPosition(function(position) {
var myVar1, myVar2, myVar3; // Define has many variables as you want here
// From here you can pass the position, as well as any other arguments
// you might need.
self.foundLoc(position, self, myVar1, myVar2, myVar3)
}, this.noloc, { timeout : 3});
},
foundLoc : function(position, self, myVar1, myVar2, myVar3) {},
};
希望这对可能偶然发现此问题的其他人有所帮助。
关于javascript - getCurrentPosition成功回调如何传参?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14740942/