我有一个很简单的JS使用navigator。geolocation。getcurrentposition jammy。

$(document).ready(function(){
  $("#business-locate, #people-locate").click(function() {
    navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
  });

  navigator.geolocation.getCurrentPosition(foundLocation, noLocation);

  function foundLocation(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    var userLocation = lat + ', ' + lon;
    $("#business-current-location, #people-current-location").remove();
    $("#Near-Me")
      .watermark("Current Location")
      .after("<input type='hidden' name='business-current-location' id='business-current-location' value='"+userLocation+"' />");
    $("#people-Near-Me")
      .watermark("Current Location")
      .after("<input type='hidden' name='people-current-location' id='people-current-location' value='"+userLocation+"' />");
  }
  function noLocation() {
    $("#Near-Me").watermark("Could not find location");
    $("#people-Near-Me").watermark("Could not find location");
  }
})//end DocReady

基本上,这里发生的事情是我们得到当前位置,如果它得到了,两个“水印”被放置在两个字段中,表示“当前位置”,两个隐藏字段被创建,并使用latong数据作为它们的值(它们在开始时被删除,这样它们就不会每次都重复)。还有两个按钮,它们有一个点击功能,做同样的事情。 不幸的是,每隔三次左右,它就会奏效。 这里有什么问题?


当前回答

我把这个贴在这里,以防对任何人有用……

在iOS Safari上,如果我有一个活动的navigator. gelocation . watchposition函数在运行,那么调用navigator. gelocation . getcurrentposition会超时。

使用clearWatch()像这里描述的那样正确地启动和停止watchPosition,可以工作:https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition

其他回答

我把这个贴在这里,以防对任何人有用……

在iOS Safari上,如果我有一个活动的navigator. gelocation . watchposition函数在运行,那么调用navigator. gelocation . getcurrentposition会超时。

使用clearWatch()像这里描述的那样正确地启动和停止watchPosition,可以工作:https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition

以下是我的解决方案,感谢一个闭包:

  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);

我在使用Firefox的响应式设计模式时就遇到过这种情况。已经提交了一份错误报告。现在,在使用地理定位API时不要使用响应式设计模式。

也许这对一些人有帮助,在Android上我也有同样的问题,但我用它解决了 setTimeout 内部 document.ready 所以它为我工作第二,你必须增加超时以防万一,如果用户允许他的位置几秒钟后,所以我保持它为60000毫秒(1分钟)允许我的成功函数调用,如果用户点击允许按钮在1分钟内。

在我们的例子中,它总是第一次工作,但重新运行函数3-4次以上,它就失败了。

简单的解决方法:将其值存储在LocalStorage中。

之前:

navigator.geolocation.getCurrentPosition((position) => {
  let val = results[0].address_components[2].long_name;
  doSthWithVal(val);      
}, (error) => { });

后:

if(localStorage.getItem('getCurrentPosition') !== null) {
  doSthWithVal(localStorage.getItem('getCurrentPosition'));
}
else {
  navigator.geolocation.getCurrentPosition((position) => {
      let val = results[0].address_components[2].long_name;
      localStorage.setItem('getCurrentPosition',val);
      doSthWithVal(val);      
  }, (error) => { });
}