它以前在我的代码中起作用,但是随着最近的更改,我使它似乎已损坏。我尝试在网上寻找解决方案,但是这对于我来说似乎是我需要的。我想要的只是一个“ alert();”。在浏览器“地理位置服务”请求被拒绝时显示。这是没有添加警报的代码。谢谢。

      if (navigator.geolocation) {
            // Locate position
            navigator.geolocation.getCurrentPosition(displayPosition);
        } else {
            alert('It seems like Geolocation, which is required for this page, is not enabled in your browser. Please use a browser which supports it.');
        }
        blacklisted_areas = {
            'area 51': [1, 2],
            'pink unicorn zoo': [1, 2],
        };
        // Success callback function
        function displayPosition(pos) {
  var mylat = pos.coords.latitude;
  var mylong = pos.coords.longitude;
  var thediv = document.getElementById('locationinfo');
  thediv.innerHTML = '<p>Your longitude is :' + mylong + ' and your latitide is ' + mylat + '</p>';

  var blacklisted = false;
  for (let x of Object.values(blacklisted_areas)) {
    if (mylat === x[0] && mylong === x[1]) {
      blacklisted = true;
    }
  }
  if(!blacklisted){
    window.location="insertURLHere";
  }


}

最佳答案

只需处理错误情况:

navigator.geolocation.getCurrentPosition(displayPosition, onError);
function onError (error) {
    if (error.code == error.PERMISSION_DENIED)
       alert("you denied geolocation");
};

关于javascript - 地理位置服务被拒绝时,如何添加警报?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44962570/

10-11 08:19