navigator.geolocation getCurrentPosition not updating in Chrome Mobile

前端 未结 3 2201
粉色の甜心
粉色の甜心 2020-12-15 01:07

I have created a site (can be accessed at http://dev.gkr33.com) which is designed for a smartphone and attempts to use the navigator.geolocation api and grab your position v

相关标签:
3条回答
  • 2020-12-15 01:26

    getCurrentLocation() no longer works on insecure origins in Chrome browsers. Switch to a secure original (HTTPS) to enable.

    0 讨论(0)
  • 2020-12-15 01:27

    I finally found a working version for firefox, chrome & default navigator in android (4.2 tested only):

    function getGeoLocation() {
            var options = null;
            if (navigator.geolocation) {
                if (browserChrome) //set this var looking for Chrome un user-agent header
                    options={enableHighAccuracy: false, maximumAge: 15000, timeout: 30000};
                else
                    options={maximumAge:Infinity, timeout:0};
                navigator.geolocation.getCurrentPosition(getGeoLocationCallback,
                        getGeoLocationErrorCallback,
                       options);
            }
        }
    
    0 讨论(0)
  • 2020-12-15 01:31

    I never got to the bottom of this issue, but I got a way around the problem by utilising the watchPosition call, and wrapping this in a 5 second wait before clearing the watchID. Check the code below:

    var options = { enableHighAccuracy: true, maximumAge: 100, timeout: 50000 };
    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.

    Hope this helps someone with the same issue :)

    0 讨论(0)
提交回复
热议问题