这都是小提琴:
https://jsfiddle.net/a4q0zjm8/
以及此在线示例:
https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation
代码:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<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>