'Access "Browser" location

We all have seen this prompt:

enter image description here

As far as I know, this is not IP-based location. This is device-based location.

I don't want IP-based location because 1) It's not reliable and 2) If the user browses my website with a VPN, the location data is absolutely wrong.

I've searched PyPi.org and DjangoPackages.org but didn't find anything to implement that in my Django app.

Is there any solution?



Solution 1:[1]

To get the browser to ask the user for geolocation you must use javascript.

This snippet from w3schools demonstrates this.

The latitude and longitude can you then pass forward to the backend.

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<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 + 
  "<br>Longitude: " + position.coords.longitude;
}
</script>

</body>
</html>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adilius