使用 HTML5 获取用户的经纬度 [ 新手入门 ]
<!DOCTYPE html>
<html>
<head>
<title>Geolocation Example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>Click the button to get your current location:</p>
<button onclick="getLocation()">Get Location</button>
<p id="demo"></p>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
document.getElementById("demo").innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
document.getElementById("demo").innerHTML = "Latitude: " + lat + "<br>Longitude: " + lng;
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
document.getElementById("demo").innerHTML = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
document.getElementById("demo").innerHTML = "Location information is unavailable.";
break;
case error.TIMEOUT:
document.getElementById("demo").innerHTML = "The request to get user location timed out.";
break;
case error.UNKNOWN_ERROR:
document.getElementById("demo").innerHTML = "An unknown error occurred.";
break;
}
}
</script>
</body>
</html>
共 0 条回复
没有找到数据。
PHP学院的中学生
注册时间:2018-10-23
最后登录:2024-09-23
在线时长:168小时13分
最后登录:2024-09-23
在线时长:168小时13分
- 粉丝29
- 金钱4725
- 威望30
- 积分6705