JavaScript Geolocation failing in all Android browsers - working in iOS and PC

10,804

Solution 1

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 is working fine. Check if the device has settings of share location false in device. When a page script is requesting for location browser will ask for user permission in android device browser. May be you have clicked never share your location. Please check it by clearing the browser settings in your device. I guarantee that there is no geolocation api problem in android 2.3+ without messing up with security settings.

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

Solution 2

You must use HTTPS

Starting with Chrome 50, Chrome no longer supports obtaining the user’s location using the HTML5 Geolocation API from pages delivered by non-secure connections. This means that the page that’s 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

Solution 3

After searching around for a solution to the same problem, having come to the conclusion that the timeout needs to 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');
        }

    };

Solution 4

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

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

Another workaround is to use firefox as the browser.

Share:
10,804
TheLettuceMaster
Author by

TheLettuceMaster

Android (Java) Developer for fun, and Professional Full Stack Developer using python, ruby, JavaScript, SQL and php.

Updated on June 12, 2022

Comments

  • TheLettuceMaster
    TheLettuceMaster almost 2 years

    Here is my very basic 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 in iOS (Safari and Chrome); and all PC browsers I've tried.

    On Android, I've tried the stock HTC Browser, Chrome and Dolphin. The Satellites look like it is searching then, it stops. I don't even recall it asking for my permission to use geolocation (could have overlooked that part)

    UPDATE: It does work on my Nexus 10 Chrome browser. But not my HTC One X.

    UPDATE 2 - This appears to ONLY be happening on one Android device, an AT&T HTC One X. All other Android devices, PC browsers and iOS work fine. On the One X I get the Error Code: Timeout. Also, GPS works fine on this device otherwise.

  • Exception
    Exception over 10 years
    @KickingLettuce Still if you write some code and want to validate in all devices, please let me know
  • delkant
    delkant almost 8 years
    This was something that solved a problem to me in the past. Have you tried it? is it working? I can take a look again if you need some help.. pls let me know.
  • Qback
    Qback about 6 years
    What about Firefox on mobile? I have no location prompt on both.
  • Qback
    Qback about 6 years
    ok, I found it out. Here you can find list of supported browsers and information if they support only https.