navigator.geolocation.getCurrentPosition doesn't work on android google chrome

Andrey Koltsov picture Andrey Koltsov · Jun 27, 2013 · Viewed 56k times · Source

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.

Answer

Adrian picture Adrian · Jul 3, 2013

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.