我通过嵌入其他功能的HTML文件中有一些GPS位置请求。我遇到的问题是,当应用程序打开时,如果位置未启用(或者在请求被触发后关闭位置)。然后我将不得不重新启动应用程序,以便位置请求再次工作。
如果它失败一次,那么即使该位置被切换回来,之后的每一个请求都将是一个错误。
为什么会这样?
var Geo11={};
navigator.geolocation.getCurrentPosition(success11,error11, { enableHighAccuracy: true, maximumAge: 5000, timeout: 20000 });
//Get the latitude and the longitude;
function success11(position) {
Geo11.lat = position.coords.latitude;
Geo11.lng = position.coords.longitude;
calcRoute(Geo11.lat, Geo11.lng);
}
function error11(error){
alert('Please check your GPS setting, then reload.');
}
答案 0 :(得分:0)
Firstly, you have to do proper error handling. See the snippet.
canPerformUnwindSegueAction
Secondly, after error handling you would also have to implement reload.
Note: Since GPS is turned on from an OFF state GPS hardware component is (re)booting and in the absence of proper timeout and reload mechanism, it won't work as intended.
That being said. In my opinion in your use case it would be better if you just ask the user for location permission before calling geolocation. There's a very good example here for your reference.
Also, this answer is worth reading.