我有一个由按钮点击触发的功能,可以检查地理位置。当地理位置打开时,它在手机上正常工作,当关闭时,正如预期的那样显示一条消息。首先关闭手机的位置服务,点击按钮(按预期弹出消息),然后如果用户在应用程序仍处于打开状态时重新打开位置服务,再次单击按钮,仍然会出现问题弹出“无位置服务”消息。
有没有办法检查每次点击按钮时手机的位置服务是打开还是关闭?在Android和IOS上获得相同的结果。
代码:
<div id="content">
Stuff should be placed here.
</div>
<br/>
<br/>
<br/>
<ul>
<li onmouseover="hover('Apples are delicious')">Apple</li>
<li onmouseover="hover('oranges are healthy')">Orange</li>
<li onmouseover="hover('Candy is the best')">Candy</li>
</ul>
答案 0 :(得分:1)
从另一篇SO帖https://stackoverflow.com/a/14862073/6539349
中查看此答案您必须检查http://www.w3schools.com/html/html5_geolocation.asp
中建议的错误function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
getCurrentPosition()方法的第二个参数
showError
用于处理错误。如果无法获取用户的位置,它指定要运行的函数:
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}