Geolocation JavaScript error in all Android browsers - work in iOS and PC

Here is my very simple code:

if (navigator.geolocation) { // WILL GET TO THIS POINT WITH TEST `ALERT()` navigator.geolocation.getCurrentPosition( // WILL NOT GET OT THIS POINT IN ANDROID BROWSER function(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; }, showError, { enableHighAccuracy: true, timeout : 5000, maximumAge: 0 } ); } else { return alert('No Geolocation Support.'); } }; 

It works great on iOS (Safari and Chrome); and all the PC browsers I tried.

On Android, I tried using the HTC Chrome, Chrome, and Dolphin browser. Satellites look like they are looking then they are stopping. I don’t even remember asking for my permission to use geolocation (could have missed this part)

UPDATE It works in my Chrome Nexus 10 browser. But not my HTC One X.

UPDATE 2 . It looks like ONLY on one Android device, AT & T HTC One X. All other Android devices, browsers for PC and iOS are working fine. In One X, I get the error code: Timeout. In addition, GPS is working fine on this device otherwise.

+6
source share
7 answers

Your code is absolutely correct. I have tested this in Android 2.3 + to latest version in different devices including HTC, Samsung, Asus tablets and Samsung tablets . Even in tablets, it works fine. Check if the device has share location false settings on the device. When the script page requests a location browser, the user’s permission will be requested in the browser of the Android device. Maybe you clicked, never share your location. Check it out by clearing the browser settings on your device. I guarantee that in android 2.3+ there is no geolocation api problem without breaking security settings.

And if the problem still persists, and you want to add at least some of the polyfill, then check this https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills and get lucky. They calculate the location based on your IP usig geoIP database .

+5
source

You must use https

Starting with Chrome 50, Chrome no longer supports retrieving user locations using the HTML5 binding API on pages supplied with insecure connections. This means that the page making the geolocation API call must be served from a secure context such as HTTPS.

https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only?hl=en

+6
source

After finding a solution to the same problem, having come to the conclusion that the timeout should be set much higher for Android devices, I implemented the following as a base:

 function setGeoLocation(){ var ua = navigator.userAgent.toLowerCase(), isAndroid = ua.indexOf("android") > -1, geoTimeout = isAndroid ? '15000' : '1000'; function success (position) { console.log(position); }; function error (err) { console.log('error message'); } if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(success, error, {enableHighAccuracy: true, maximumAge: 3000, timeout:geoTimeout}); } else { error('Location services must be enabled to use this'); } }; 
+1
source

I ran into the same problem with a program that does not interact with the website. It has a URL where it gets its code, but after loading the HTML page and the .js files, it does not contact the host. This program worked for several years and simply stopped working. The problem was that the new version of Chrome disabled the function navigator.geolocation.getCurrentPosition (). I changed the site to allow access to SSL, and the program now works as before.

I'm really surprised that Chrome has disabled this feature on a mobile device.

Another workaround is to use Firefox as a browser.

+1
source

I also have a geolocation API project in HTML5. I tested ZTE V880 (ROM CM7 2.3), ZTE V980 ROM 4.0, Huawei P6 ROM4.2, Samsung Galaxy Note I (ROM 2.3, ROM 4.1), I found that the location service can be solved on any device. But I can not work on Galaxy Note, I am with Rom2.3, wifi and gps.

But some device can track location when using Opera browser, for example ZTE V980 ROM4.0

0
source

Try this : to call watchPosition and wrap it for 5 seconds before clearing 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 have not played with the β€œoptions” values ​​or the timeout delay at the moment, but the code above returns accurate location information on each platform that I tried.

0
source

I also had the same problem and resolved it by setting enableHighAccuracy: false . Some devices do not support GEO locations if enableHighAccuracy set to true

0
source

Source: https://habr.com/ru/post/951228/


All Articles