tags:

views:

25

answers:

2

navigator.geolocation.getCurrentPosition returns the approx lat/long information if I connect to the internet via Wireless. However the method call fails when connected via Network cable. Is this by design or am I missing something? Below is the sample html page and I am using Chrome 5 to test it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title>Geocode Test</title>
    <script type="text/javascript">
        function getGeoLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    alert(position);
                },
                function () {
                    alert("oops");
                });
            }
        }
    </script>
</head>
<body>
    <h1>Get Current Location</h1>
    <button id='btnGetLocation' onclick='getGeoLocation()'>Get Current Location</button>
</body>
</html>

TIA

A: 

I've not seen any difference in the ability to Geolocate depending on network. For a more comprehensive method of getting geolocation data, see the Google Maps Geolocation Code Example. What you should do is compare the wireless IP address with the wired IP address. Perhaps there's some NAT translation going on, or alternatively the wireless IP value hasn't been put into the IP-to-geo database you're accessing.

Tony Miller
+1  A: 

The Geolocation API implementation on Chrome prefers to use WiFi triangulation rather than IP geolocation (contra Tony Miller's answer), though I believe Google Location Services does fall back on IP address when it has to, or at least uses the IP address to confirm its guess.

When you're connected via a network cable do you still have WiFi running? Commonly these services don't require that you be connected to a particular WiFi network, they just use a full list of all the visible wireless networks. But if you turned off WiFi on your computer or turned off the available wireless networks nearby, that would largely prevent navigator.geolocation from working.

npdoty
@npdoty Thanks for the clarification. From my experiments with Chrome, FireFox and, Safari, I discovered that with WiFi turned off and network cable connected, the call to navigator.geolocation either returns JSON Parser error (Chrome) or timesout (FF and Safari).
rams
Good testing @rams, thank you! Those don't really seem like ideal ways to fail...
npdoty