最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Aug 8, 2013 at 15:09 TheLettuceMaster asked Aug 7, 2013 at 15:10 TheLettuceMasterTheLettuceMaster 15.7k50 gold badges158 silver badges266 bronze badges 4
  • 1 I think this issue have been faced by many users, first of all, you have to correct else part, 'else { alert('No Geolocation Support.'); return ; } ' as this will give you alert, I believe direct return alert will give error and not standardize/proper way of passing method in return. As i think you will be able to see the alert at least after changing it. – MarmiK Commented Aug 19, 2013 at 11:07
  • Also in modern browser you can use this to check support, 'if (Modernizr.geolocation){//your code}' also it is possible that calling getCurrentPosition() with enableHighAccuracy:true will fail, but calling with enableHighAccuracy:false would succeed. – MarmiK Commented Aug 19, 2013 at 11:13
  • IF your broswer do not return anything The solution to this is fix of browser as the browser do not support this call, or does not return anything. refer this link.. github.com/rhomobile/rhodes/blob/… this file is a fix to this and will return js call a message that will help.. – MarmiK Commented Aug 19, 2013 at 11:33
  • Could it be that the device has GPS disabled? You are asking for high-accuracy position, it will demand GPS to be enabled. – Delyan Commented Aug 19, 2013 at 13:43
Add a comment  | 

7 Answers 7

Reset to default 7

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

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.

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');
        }

    };

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.

I also have a project about geolocation API in HTML5. I tested ZTE V880(ROM CM7 2.3),ZTE V980 ROM 4.0, Huawei P6 ROM4.2, Samgsung Galaxy Note I (ROM 2.3,ROM 4.1), found that the location service can be solve in some device. But cannot work on Galaxy Note I with Rom2.3, wifi & gps.

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

Try this: 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.

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

发布评论

评论列表(0)

  1. 暂无评论