gpt4 book ai didi

javascript - 地理定位脚本不起作用?谁能帮助我我的代码出了什么问题

转载 作者:行者123 更新时间:2023-12-02 21:41:44 25 4
gpt4 key购买 nike

我想要当前位置的纬度和经度。

我的代码

    <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) {
demo.innerHTML ="<form name='formname' action='weather.php' method='GET'> <input type='text' name='lat' id='lat' value='"+position.coords.latitude+"'><br><input type='text' name='long' id='long' value='"+position.coords.longitude+"'><input type='submit' name='submit' value='submit'/></form>";
}
</script>

<div>
<button onclick="getLocation()" name="getlocation" id="getlocation" value="getlocation">Get location</button><br>

<div id='demo'></div>
</div>

相同的代码在本地主机中运行良好。但在服务器上,它不起作用。

最佳答案

只有当用户允许网站知道其位置时,它才会起作用。否则 getCurrentPosition() 将返回“未定义”

关于javascript - 地理定位脚本不起作用?谁能帮助我我的代码出了什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60349907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com