Home > Mobile >  Why is JavaScript geolocation only Approximate?
Why is JavaScript geolocation only Approximate?

Time:02-01

No matter how I use the JavaScript:

position.coords.latitude;
position.coords.longitude;

The returned coordinates are always at least a few city blocks off. So - it will show the "start point" or "Your location" as a few streets over?

This approximate result seems new, as the code I used before produced a more accurate result.

I have tested on all browser's from a "https" web page.

<script>
var x = document.getElementById("demo");

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  x.innerHTML = "Latitude: "   position.coords.latitude   '/'   position.coords.longitude;
}
</script>

Has something changed? Why is the produced coordinates only approximate?

CodePudding user response:

This is a tradeoff between speed and accuracy. Gelocation.getCurrentPosition() at MDN details the optional parameter enableHighAccuracy

CodePudding user response:

JavaScript geolocation is only an approximation of the device's actual location because it relies on the device's built-in GPS, Wi-Fi, IP address lookup and/or network triangulation to determine your location. Additionally, the Geolocation API provides an estimated location, not an exact location. The accuracy of the location information can vary greatly and can be influenced by various factors, including the type of device, its settings, and the environment (buildings, trees, etc) in which it is used.

I used your function and indeed the latitude and longitude coordinates were not exact, there is a difference of a few meters (more than 30-40).

  • Related