Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 574
Andrey Koltsov Avatar asked Jun 27 '13 16:06

Andrey Koltsov


People also ask

What is navigator Geolocation getCurrentPosition?

Description. The getCurrentPosition method retrieves the current geographic location of the device. The location is expressed as a set of geographic coordinates together with information about heading and speed. The location information is returned in a Position object.

Why is Geolocation not accurate?

There is no guarantee the geolocation information will be accurate, because this API can also use the IP address to infer user location in the absence of more accurate technologies, such as GPS receivers on the client's device. The browser exposes this API via the navigator. geolocation property.

What is navigator Geolocation?

The Navigator. geolocation read-only property returns a Geolocation object that gives Web content access to the location of the device. This allows a Web site or app to offer customized results based on the user's location.


2 Answers

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.

like image 180
Adrian Avatar answered Sep 22 '22 08:09

Adrian


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.

like image 20
delkant Avatar answered Sep 18 '22 08:09

delkant