【发布时间】:2019-05-22 10:11:57
【问题描述】:
我想获取访问我网站的用户的地理位置。我喜欢这个简单的 'https://www.w3schools.com/html/html5_geolocation.asp' 代码,当我自己使用 try it 时,它显示了纬度和经度。我在我的网站上试过了,没问题。但另一个尝试给了我 NaN。来自 w3school 网站和我的网站。请问,我该如何解决这个问题?
<html>
<body>
<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>
</body>
</html>
<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>
【问题讨论】:
标签: javascript html