0

I run the code shown below from different places but this results in same latitude and longitude value at different places.

 <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title></title>
        </head>
        <body onload="getLocation()">
        <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= position.coords.latitude;
    x.innerHTML= position.coords.longitude;
    var lat=position.coords.latitude;
    var lon=position.coords.longitude;
    }
    </script>
    **strong text**</body>
    </html>

Any help would be appreciated.

6
  • @Phil I've tried to read this a few times already and I still don't know what the author wants :( Commented Oct 16, 2014 at 18:42
  • I want to know that when i m trying to access location value(latitude & longitude) at different places using this code,this code results same value although i change my place.. sorry for poor representation of my question. Commented Oct 16, 2014 at 18:49
  • @TahminaKhanamUrmi how much was the distance (approximately) between the places you tried your code? Commented Oct 16, 2014 at 19:08
  • @Attila Herbert it's about 40 km. Commented Oct 16, 2014 at 19:20
  • @TahminaKhanamUrmi Well that's strange. Your code is OK, but either your device's GPS just went full retard, or there is some more seroius problem. If you can, try it on another device. Commented Oct 16, 2014 at 19:22

1 Answer 1

1

In short, the problem isn't with your code, but most likely with your device. I can tell you're using the demo code from the W3Schools article on the geolocation API. That code seems to work fine for me, at least as much as I would expect.

Geolocation isn't terribly precise in all cases. According to MDN, the method use to determine location will be the most accurate available for your device at that time. If you're testing on a desktop browser, the results might be no more accurate than to the nearest city, and in some cases even more inaccurate than that (My home in Michigan registers as the middle of New York state, for some weird reason). If you test on a 3g or 4g phone, you might get better results, down to the nearest broadcast area. If you test on a phone with GPS, you might get highly accurate data, where you'll notice a difference if you move even a few feet or so. I say 'might' for these cases, because location data is a huge privacy concern area, and there's a lot of things that might interfere with geolocation to protect a user's privacy.

If you're seeing the same result from relativly small moves, this would easily be the cause. If you've travelled a few hundred miles to run your code elsewhere, then I would have to admit that there's something

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. the distance i have tried all about 40 km but the result is exactly same. i clear all cached data from my browser before testing the code on browser. :(
Geolocation without GPS goes off of your internet connection. If you're testing through a VPN, a proxy, or just hooked through the same ISP (depending on your provider), then the locations may look the same no matter where you are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.