36

This code:

navigator.geolocation.getCurrentPosition(
                    function(position) {
                        alert(position.coords.latitude, position.coords.longitude);
                    },
                    function(error){
                        alert(error.message);
                    }, {
                        enableHighAccuracy: true
                        ,timeout : 5000
                    }
            );

https://jsfiddle.net/FcRpM/ works in Google Chrome at my laptop, but on mobile HTC one S (android 4.1, GPS off, location via mobile networks and wifi enabled), connected to internet via WiFi.

  1. Default browser works fine.
  2. Google Chrome, Opera, Yandex.browser for android fails with "Timeout expired".

other android apps locates me correct.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
Andrey Koltsov
  • 1,956
  • 6
  • 24
  • 43
  • On my HTC one S there is the same problem. I see it is reported here: https://code.google.com/p/chromium/issues/detail?id=175909 – Baast Aug 19 '13 at 15:57

7 Answers7

20

You can try this. It seems to work on my device (Samsung Galaxy Nexus running Chrome 27.0.1453.90 on Wi-Fi (no data connection, no GPS on))

navigator.geolocation.getCurrentPosition(
    function(position) {
         alert("Lat: " + position.coords.latitude + "\nLon: " + position.coords.longitude);
    },
    function(error){
         alert(error.message);
    }, {
         enableHighAccuracy: true
              ,timeout : 5000
    }
);

The problem is that alert only takes strings (in it's original form) however you are passing 2 doubles. Modify the alert box for example to alert('Hey', 'Hello'); and the output will be only Hey. Change the , to + and you'll get the concatenated strings HeyHello. You can't use a + sign inside the alert as the equation will be first executed and then displayed.

Hope this makes it clear.

Adrian
  • 609
  • 2
  • 11
  • 22
  • 2
    problem not in alert('Hey', 'Hello'); that I see one line, problem in that code doesn't return location. Your code shows "Timeout expired" alert HTC One S Chrome Chrome 27.0.1453.90 – Andrey Koltsov Jul 05 '13 at 05:28
  • 1
    This might sound crazy, but are the location services turned on on your device? I've just tried the same code again without having the Google Location Access turned on and I got the error you mentioned. Within Chrome, go to Settings -> Content Settings -> Google location settings and see what your browser settings are – Adrian Jul 05 '13 at 09:41
  • Yes, location services turned on, code works in default browser, but doesn't work in Chrome – Andrey Koltsov Jul 08 '13 at 04:59
  • Unless you've set your Chrome to not use your location, I can't seem to find any other good explanation why the above code might not work. I've tested on my device on Chrome and the native browser and works perfectly. Sorry if I can't help more. – Adrian Jul 08 '13 at 14:10
  • 4
    Seems that enable high accuracy fixed this for me. Noticed it was not hitting the GPS at all without this option set to true. – sharpper Jul 22 '13 at 22:34
8

THERE IS A WORKAROUND: to watchPosition call, and wrapping this in a 5 second wait before clearing the watchID. Code below;

var options = { enableHighAccuracy: true, maximumAge: 100, timeout: 60000 };
if( navigator.geolocation) {
   var watchID = navigator.geolocation.watchPosition( gotPos, gotErr, options );
   var timeout = setTimeout( function() { navigator.geolocation.clearWatch( watchID ); }, 5000 );
} else {
   gotErr();
}

I haven't played around with the "options" values or the timeout delay at the moment, but the above code brings back accurate positioning info on every platform I've tried.

delkant
  • 2,304
  • 1
  • 29
  • 28
  • 2
    are you sure the workaround works? I just tried it and it didn't – Guy Korland Dec 10 '14 at 11:04
  • yes, it should work, check that your browser allows location requests from the host where your scripts are being loaded. It happened to me before that my domain was in the black list of the browser so location wasn't shared to the site. – delkant Dec 11 '14 at 14:52
7

Just finished testing a bunch of mobile devices and the Javascript Geolocation. I used the example code from Google in order to make sure that the problem is not in my own code.

Chrome for Android 4.4 does not seem to work with GPS-only location services and neither does the 2.3 stock browser. They both need "High accuracy" - the use of wireless and 3G/4G networks.

The funny thing is that Firefox for Android works without any problems GPS-only. Only the stock Android browsers (Chrome + Mobile Safari) fail with GPS-only location settings.

And the rest of the gang - Lumia WP8 with GPS, Windows and Linux (both with ADSL) worked perfectly with any location settings.

karvonen
  • 658
  • 9
  • 10
4

I ran into this problem yesterday and the issue was that the Geolocation API can only be used over HTTPS. It will work on http://localhost but for other devices, you need to be on a secure connection.

Hope it helps!

Ajay Gupta
  • 1,944
  • 2
  • 21
  • 36
2

After many hours of seeking solution for error3, i only can reboot my phone, and geolocation starts work as usually. So bad...

saike
  • 906
  • 8
  • 15
2

It was working for me for every simulator but not for android devices

what worked for me was

navigator.geolocation.getCurrentPosition(
                (position) => {
                    console.log(position);
                },
                (error) => console.log(new Date(), error),
                { enableHighAccuracy: false, timeout: 5000},
            );

means instead of three argument I used only two means

{ enableHighAccuracy: false, timeout: 5000}

Sultan Ali
  • 2,497
  • 28
  • 25
-6

just add

"geolocation",
"location"

under permissions. Worked for me. :-)