HTML5 Geolocation, Full Position Display

by Matthew Schenker

HTML

<form action="geo/ruteplanlag.php" method="get">
  <p>Latitude:
    <input type="text" id="Latitude" name="Latitude" value="">
  </p>
  <p>Longitude:
    <input type="text" id="Longitude" name="Longitude" value="">
  </p>
  <p>Position:
    <input type="text" id="Position1" name="Position1" value="">
  </p>
  <input type="button" value="Get Location" onclick="getLocationConstant()" />
</form>

JavaScript

if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(onGeoSuccess, onGeoError);
    } else {
      alert("Your browser or device doesn't support Geolocation");
    }

    // If we have a successful location update
    function onGeoSuccess(event) {
      document.getElementById("Latitude").value = event.coords.latitude;
      document.getElementById("Longitude").value = event.coords.longitude;
      document.getElementById("Position1").value = event.coords.latitude + ", " + event.coords.longitude;
    }

    // If something has gone wrong with the geolocation request
    function onGeoError(event) {
      alert("Error code " + event.code + ". " + event.message);
    }