navigator.geolocation getCurrentPosition not updating in Chrome Mobile

前端 未结 3 2208
粉色の甜心
粉色の甜心 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: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 :)

提交回复
热议问题