navigator.geolocation.getCurrentPosition sometimes works sometimes doesn't

前端 未结 25 1883
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 13:33

So I have a pretty simple bit of JS using the navigator.geolocation.getCurrentPosition jammy.

$(document).ready(function(){
  $(\"#business-locate, #people-l         


        
25条回答
  •  醉梦人生
    2020-11-22 14:10

    You don't get an error message because it has no timeout by default (At least i think). I have had the same problem with firefox only for me firefox always gives an timeout. You can set a timeout yourself like this.

    My function works great in chrome but i get a timeout everytime in firefox.

        navigator.geolocation.getCurrentPosition(
            function(position) {
                //do succes handling
            },
            function errorCallback(error) {
                //do error handling
            },
            {
                timeout:5000
            }
        );
    

    I recommend to watch your errors carefully. Be expected for everything. Have a backup plan for everything. I use some default values or values from my database myself in case both google geolocations and navigator geolocations fails.

提交回复
热议问题