Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 geolocation is not accurate

I was using the following code to get my current location. But the longitude and latitude generated was not at all accurate. It was showing a location about 700 Kms away from my location. How can I make it accurate?

<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>
like image 755
the_learner Avatar asked Sep 12 '25 02:09

the_learner


2 Answers

Probably you are not using GPS. If you aren't, then navigator.geolocation.getCurrentPosition returns a position based on your ISP

like image 75
Davide Riva Avatar answered Sep 13 '25 14:09

Davide Riva


Or you could try a different geocoding provider.

There are lots to choose from:

  • https://smartystreets.com
  • http://geocoder.us
  • http://geoservices.tamu.edu/Services/Geocode/

Disclaimer: I'm a developer at SmartyStreets.

like image 36
mdwhatcott Avatar answered Sep 13 '25 15:09

mdwhatcott