HTML5 Geolocation 能否支持使用 PHP 的移动 Web 应用程序? (例如:IOS & Android)

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

我正在开发一个允许用户通过桌面和移动设备访问系统的 Web 应用程序项目。事实上,我正在开发的系统需要获取用户的位置坐标。

我可以使用桌面网络浏览器检索坐标,它们也很准确。不幸的是,当我尝试通过移动设备进行测试时,它并没有检索到它们。即使我已经启用了我的位置服务,它仍然不起作用。

Tested using IOS

这是我的代码:

<!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 php html geolocation
© www.soinside.com 2019 - 2024. All rights reserved.