navigator.geolocation.getCurrentPosition sometimes works sometimes doesn't

前端 未结 25 1922
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  Happy的楠姐
    2020-11-22 14:18

    This is the hacky way that I am getting around this, at least it works in all current browsers (on Windows, I don't own a Mac):

    if (navigator.geolocation) {
        var location_timeout = setTimeout("geolocFail()", 10000);
    
        navigator.geolocation.getCurrentPosition(function(position) {
            clearTimeout(location_timeout);
    
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
    
            geocodeLatLng(lat, lng);
        }, function(error) {
            clearTimeout(location_timeout);
            geolocFail();
        });
    } else {
        // Fallback for no geolocation
        geolocFail();
    }
    

    This will also work if someone clicks the close or chooses no or chooses the Never Share option on Firefox.

    Clunky, but it works.

提交回复
热议问题