我有一个很简单的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数据作为它们的值(它们在开始时被删除,这样它们就不会每次都重复)。还有两个按钮,它们有一个点击功能,做同样的事情。 不幸的是,每隔三次左右,它就会奏效。 这里有什么问题?


当前回答

这已经是一个老问题了,但所有的答案都不能解决我的问题,所以让我们加上我最终找到的那个。它闻起来像一种hack(它确实是一种),但在我的情况下总是有效。对你的处境也抱有希望。

//Dummy one, which will result in a working next statement.
navigator.geolocation.getCurrentPosition(function () {}, function () {}, {});
//The working next statement.
navigator.geolocation.getCurrentPosition(function (position) {
    //Your code here
}, function (e) {
    //Your error handling here
}, {
    enableHighAccuracy: true
});

其他回答

这是我解决这个问题的hack方法,至少它在所有当前的浏览器中都有效(在Windows上,我没有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();
}

如果有人点击“关闭”或选择“不”或在Firefox上选择“永不共享”选项,这也会起作用。

很笨拙,但很有效。

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

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

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

大家也一样,顺便说一句,这在Chrome(稳定,开发和金丝雀)中工作完美,只是在FF和Safari中不合适。它在我的iPhone和iPad上也很好用(Safari!)这可能是由于这个功能相对较新(即它是一个bug)。我现在花了差不多一个星期的时间在这个上面,我就是不能让它在那些浏览器上工作

以下是我的发现:

第一次调用getCurrentPosition时,它工作得很完美。任何后续调用都不会返回,也就是说,它不会触发successCallback或errorCallback函数。为了证明我的观点,我在看涨期权中添加了几个仓位选项:

 navigator.geolocation.getCurrentPosition(successCallback, errorCallback,  {timeout: 10000});

并且每次都超时(在第一次成功调用之后)。我认为我可以用maximumAge来修复它,但这似乎并不像它应该工作的那样工作:

navigator.geolocation.getCurrentPosition(successCallback, errorCallback,  {maximumAge:60000, timeout: 2000});

这应该防止实际调用getCurrentPosition函数,如果你在60秒内调用它,但它忽略了这一点(然而,这可能是因为我实际上刷新了我的页面来触发第二次调用,不确定这是否持久跨调用)

顺便说一句,即使谷歌的例子失败在这些浏览器上,这让我相信这确实是浏览器的bug,尝试一下,在Safari中加载两次,第二次就不会工作了。

如果有人找到了解决方案,请告诉我:-)

欢呼。

当getCurrentPosition失败时,使用IP地址地理位置服务作为回退方法可能是个好主意。例如,我们的api https://ip-api.io

$.getJSON("http://ip-api.io/json/",
    function(result) {
        console.log(result);
    });

这个库为地理定位调用添加了一个desiredAccuracy和maxWait选项,这意味着它将继续尝试获取位置,直到精度在指定的范围内。