每当用户单击按钮时,我都试图获取当前位置。
function checkConnection() {
// check if there is connection and call the getcurrent position function
navigator.geolocation.getCurrentPosition(onLocSuccess, onLocError);
}
function onLocSuccess(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
myPosition = new google.maps.LatLng(latitude, longitude);
//..........
//..........
// do something more
//.....
}
在浏览器上,这可以正常工作。但是在设备上(在iphone5上进行测试),在请求2-3次之后,这段代码无法获取位置。它冻结,导致应用程序冻结。
但是有趣的是,如果我在onLocSuccess内放置一个警报,它将正常工作!
function onLocSuccess(position) {
alert('this alert solves the issue');
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
myPosition = new google.maps.LatLng(latitude, longitude);
//..........
//..........
// do something more
//.....
}
有什么问题吗?
最后使用settimeout。这有效!
function onLocSuccess(position) {
setTimeout(function () {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
myPosition = new google.maps.LatLng(latitude, longitude);
//..........
//..........
// do something more
//.....
}, 50);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句