【问题标题】:Safari asks permission multiple times with geolocationSafari 通过地理位置多次请求许可
【发布时间】:2015-05-28 14:04:49
【问题描述】:

我一直在做这个:http://davidcool.com/feeds

它在除 Safari 之外的所有浏览器中都能正常工作。有时 Safari 会在无休止的循环中请求地理定位许可,有时它会按预期只请求一次,大多数情况下它会请求 3 次......所有其他浏览器都会按计划请求一次。

代码如下:

    function success(position) {
    //output lat+long data
    //console.log(position);
    //var s = document.querySelector('#status');
    //s.innerHTML = "Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude + "<br />";
    var s = document.querySelector('#status');
    if (s.className == 'success') {
        // not sure why we're hitting this twice in FF, I think it's to do with a cached result coming back    
        return;
    }
    s.innerHTML = "success";
    s.className = 'success';

    //get timezone info
    $.ajax({
          url:'https://maps.googleapis.com/maps/api/timezone/json?location=' + position.coords.latitude + ',' + position.coords.longitude + '&timestamp=' + Math.floor(position.timestamp/1000) + '&language=en&key=????',
          dataType: "json",
          async:false,
          cache:false,
          error:
            function(res){
              console.log(res);
              },
          success:
              function(res){
                //console.log(res);
                time_zone_id = res.timeZoneId;
                time_zone_name = res.timeZoneName;
                //var t = document.querySelector('#status1');
                //t.innerHTML = "Time Zone ID: " + res.timeZoneId + "<br />Time Zone Name: " + res.timeZoneName + "<br />" ;
              }
        });
    //get address info
    $.ajax({
          url:'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + ',' + position.coords.longitude + '&language=en&key=????',
          dataType: "json",
          async:false,
          cache:false,
          error:
            function(res){
              console.log(res);
              },
          success:
              function(res){
                //console.log(res);
                city = res.results[2].address_components[1].long_name;
                address = res.results[2].formatted_address;
                //var t = document.querySelector('#status2');
                //t.innerHTML = "Address: " + res.results[0].formatted_address ;
              }
        });

    //get weather info
    $('#weather').load("weather.php", {'latitude':position.coords.latitude, 'longitude':position.coords.longitude, 'local_time_zone_id':time_zone_id, 'local_time_zone_name':time_zone_name, 'city':city, 'address':address});
};

function error(msg) {
  var s = document.querySelector('#weather');
  //s.innerHTML = typeof msg == 'string' ? msg : "Failed to get location. Try refreshing.";
  s.innerHTML = typeof msg == 'string' ? msg : "Failed to get location. Try refreshing.";
  s.className = 'fail';
};

//get lat+long info
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success, error);
  //setTimeout(navigator.geolocation.getCurrentPosition(success, error), 500);
} else {
  error('not supported');
};

我查看了其他有相同问题的人,但无法找到关于为什么会发生这种情况的明确答案。有什么想法吗?

有趣的是,在我的开发 Mac 上它一直只询问一次,而在另一台开发 Mac 上它会询问多次!!!相同的操作系统和相同版本的 Safari 8.0.6。

【问题讨论】:

  • 你有没有遇到过这个问题的解决方案?我们的一个应用程序也有同样的问题。 Safari 总是问我 3 次。

标签: javascript safari geolocation


【解决方案1】:

我不是 100% 确定,但我遇到了同样的问题。然后我删除了error回调中的msg参数,现在它似乎在Safari中工作了。

所以你的error 回调应该是这样的

function error() {
    var s = document.querySelector('#weather');
    s.innerHTML = "Failed to get location";
    s.className = 'fail';
}

让我知道这是否有效!

【讨论】:

    猜你喜欢
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多