PHP学院的中学生 2023-03-09 18:36:36 590次浏览 0条回复 0 0 0
<!DOCTYPE html>
<html>
<head>
  <title>Geolocation Example</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  <p>Click the button to get your current location:</p>
  <button onclick="getLocation()">Get Location</button>
  <p id="demo"></p>

  <script>
    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, showError);
      } else {
        document.getElementById("demo").innerHTML = "Geolocation is not supported by this browser.";
      }
    }

    function showPosition(position) {
      var lat = position.coords.latitude;
      var lng = position.coords.longitude;
      document.getElementById("demo").innerHTML = "Latitude: " + lat + "<br>Longitude: " + lng;
    }

    function showError(error) {
      switch(error.code) {
        case error.PERMISSION_DENIED:
          document.getElementById("demo").innerHTML = "User denied the request for Geolocation.";
          break;
        case error.POSITION_UNAVAILABLE:
          document.getElementById("demo").innerHTML = "Location information is unavailable.";
          break;
        case error.TIMEOUT:
          document.getElementById("demo").innerHTML = "The request to get user location timed out.";
          break;
        case error.UNKNOWN_ERROR:
          document.getElementById("demo").innerHTML = "An unknown error occurred.";
          break;
      }
    }
  </script>
</body>
</html>
    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册