我有一个很简单的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

其他回答

这已经是一个老问题了,但所有的答案都不能解决我的问题,所以让我们加上我最终找到的那个。它闻起来像一种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
});

感谢每个人的投入,这对我很有帮助。

除了必须使用watchPosition()而不是getCurrentPosition()之外,我还发现需要将调用从document.ready()内移动到头部。

我一直有类似的问题,并一直在研究浏览器对getCurrentPosition的调用频率有限制的可能性。似乎我可以经常得到一个位置,但如果我刷新页面马上就会超时。如果我等一会儿,我通常可以再次得到一个位置。这通常发生在FF上。在Chrome和Safari中,我还没有注意到getCurrentPosition超时。只是一个想法……

虽然我找不到任何文档来支持这一点,但这是我经过多次测试后得出的结论。也许其他人有这方面的信息?

你不会得到一个错误消息,因为默认情况下它没有超时(至少我认为)。我用firefox也遇到过同样的问题,只是对我来说,firefox总是超时。您可以像这样自己设置一个超时。

我的功能在chrome中工作得很好,但我每次在firefox中都会超时。

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

我建议你仔细观察你的错误。一切都被期待。为每件事都准备一个备份计划。我使用一些默认值或值从我的数据库自己的情况下谷歌地理位置和导航地理位置失败。

@brennanyoung的回答很好,但如果你想知道在失败的情况下该怎么做,你可以使用IP地理定位API,如https://ipinfo.io(这是我的服务)。这里有一个例子:

function do_something(coords) {
    // Do something with the coords here
    // eg. show the user on a map, or customize
    // the site contents somehow
}

navigator.geolocation.getCurrentPosition(function(position) { 
    do_something(position.coords);
    },
    function(failure) {
        $.getJSON('https://ipinfo.io/geo', function(response) { 
        var loc = response.loc.split(',');
        var coords = {
            latitude: loc[0],
            longitude: loc[1]
        };
        do_something(coords);
        });  
    };
});

详情见https://ipinfo.io/developers/replacing-getcurrentposition。