无法获取 html5 地理位置

Posted

技术标签:

【中文标题】无法获取 html5 地理位置【英文标题】:unable to get html5 geolocation 【发布时间】:2017-10-26 08:38:39 【问题描述】:

我正在学习 html5 geolocation api,我写了这个简单的代码来练习。但是我不断收到“位置信息不可用”,我不知道为什么。我对此很陌生,希望能得到一些帮助。

这是我的代码。 var userLocation = document.querySelector(".location");

document.getElementById("btn").addEventListener("click", function()
  if(navigator.geolocation)
  navigator.geolocation.getCurrentPosition(showPosition, showError);
    userLocation.textContent = "Checking Location...";
  
  else 
    userLocation.textContent = "Unable to retrieve location..";
  
);

function showPosition(position)
  userLocation.textContent = position.coords.longitude;


function showError(error)
   switch(error.code)
     case error.PERMISSION_DENIED:
            alert("User denied the request for Geolocation.");
            break;
        case error.POSITION_UNAVAILABLE:
            alert("Location information is unavailable.");
            break;
        case error.TIMEOUT:
            alert("The request to get user location timed out.");
            break;
        case error.UNKNOWN_ERROR:
            alert("An unknown error occurred.");
            break;
   


【问题讨论】:

【参考方案1】:

确保您通过 httpS 加载页面,同时确保您从未拒绝过位置权限。

【讨论】:

感谢您的回复,原来是我的 PCI 卡无法与 linux ubuntu 一起使用的问题。不过感谢您的帮助。

以上是关于无法获取 html5 地理位置的主要内容,如果未能解决你的问题,请参考以下文章