Navigator.getLocation.getCurrentPosition在Android手机上不起作用

问题描述 投票:0回答:1

我有js代码,该代码会使用户久久,但是问题是当我调用js函数时,它无法在使用Chrome的Android浏览器上运行。我的android移动位置也已打开,并且也允许访问此网页。我尝试使用navigator.geolocation.getCurrentPosition(showPosition,showError,{ enableHighAccuracy: true,timeout : 5000,maxAge:1000});,它显示The request to get user location timed out,超时错误

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

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;
}
    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;
  }
}
</script>

</body>
</html>
javascript html geolocation
1个回答
0
投票

阅读了很多关于我的问题的文章后,我发现这篇文章非常有用,并且也解决了我的问题。Here is the Link

© www.soinside.com 2019 - 2024. All rights reserved.