Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.3 kB
8
Indexable
Never
<p id="deliveryLocation">Deliver at Location: <span id="userLocation"></span></p>

<script>
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(showPosition, handleError);
} else {
  document.getElementById("userLocation").innerText = "Geolocation is not supported by this browser.";
}

function showPosition(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;

  var userLocationElement = document.getElementById("userLocation");
  userLocationElement.innerText = "Latitude: " + latitude + ", Longitude: " + longitude;
}

function handleError(error) {
  var userLocationElement = document.getElementById("userLocation");

  switch (error.code) {
    case error.PERMISSION_DENIED:
      userLocationElement.innerText = "User denied the request for Geolocation.";
      break;
    case error.POSITION_UNAVAILABLE:
      userLocationElement.innerText = "Location information is unavailable.";
      break;
    case error.TIMEOUT:
      userLocationElement.innerText = "The request to get user location timed out.";
      break;
    case error.UNKNOWN_ERROR:
      userLocationElement.innerText = "An unknown error occurred.";
      break;
  }
}
</script>
Leave a Comment