const output = document.createElement("pre");
document.body.appendChild(output);

if (!("geolocation" in navigator)) {
  output.textContent = "Geolocation not supported.";
} else {
  output.textContent = "Requesting location...";
  navigator.geolocation.getCurrentPosition(
    (pos) => {
      const { latitude, longitude, accuracy } = pos.coords;
      output.textContent = `Latitude: ${latitude}
Longitude: ${longitude}
Accuracy: ${accuracy} m`;
    },
    (err) => {
      output.textContent = "Error: " + err.message;
    },
    { enableHighAccuracy: true, timeout: 10000 }
  );
}