navigator.geolocation.getCurrentPosition sometimes works sometimes doesn't

前端 未结 25 1916
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  旧时难觅i
    2020-11-22 14:14

    here is my solution thanks to a closure :

      function geoloc(success, fail){
        var is_echo = false;
        if(navigator && navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(
            function(pos) {
              if (is_echo){ return; }
              is_echo = true;
              success(pos.coords.latitude,pos.coords.longitude);
            }, 
            function() {
              if (is_echo){ return; }
              is_echo = true;
              fail();
            }
          );
        } else {
          fail();
        }
      }
    
      function success(lat, lng){
        alert(lat + " , " + lng);
      }
      function fail(){
        alert("failed");
      }
    
      geoloc(success, fail);
    

提交回复
热议问题