简体   繁体   中英

javascript issue with navigator.getCurrentPosition when device location is disabled - no callback

I am trying to get the user location via

navigator.geolocation.getCurrentPosition(this.check_location.bind(this), this.on_get_position_error.bind(this), { enableHighAccuracy: true, timeout: 5000 });

it works perfectly most of the time. But if the geolocation is disabled in the device settings (for example on iPhone : Settings App > Privacy > Location services > OFF) , Safari or Chrome won't call any callback (neither this.check_location() nor this.on_get_position_error() ).

Any idea ?

Thanks

Ok so i fixed it. I don't understand why but adding a 0-delay timeout makes it work :

setTimeout(function() {

navigator.geolocation.getCurrentPosition(this.check_location.bind(this), this.on_get_position_error.bind(this), { enableHighAccuracy: true, timeout: 5000 });

}.bind(this), 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM