如何通过隐藏表单输入获取用户的经纬度

时间:2017-04-16 17:21:34

标签: javascript php jquery html

当我在我的网站上填写表格时,我希望得到用户的经纬度..当我使用w3school的代码时,我的网站没有显示任何结果,因为可能是服务器阻止...你能帮助我获得另一个请问方法?

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}
</script>

1 个答案:

答案 0 :(得分:0)

在jsbin上工作正常。

请在w3schools

查看浏览器支持 w3schools也提到了 -

  

从Chrome 50开始,Geolocation API仅适用于安全的上下文,例如HTTPS。