<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>经纬度坐标</title>
<style>
body {
background-color: #fff;
}
</style>
</head>
<body>
<p id="geo_loc"></p>
<script>
function getElement(id) {
return typeof id === 'string' ? document.getElementById(id) : id;
}
function show_it(lat, lon) {
var str = '您当前的位置,维度:' + lat + ',经度:' + lon;
getElement('geo_loc').innerHTML = str;
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
show_it(position.coords.latitude, position.coords.longitude);
},
function(err) {
getElement('geo_loc').innerHTML = err.code + "|" + err.message;
});
} else {
getElement('geo_loc').innerHTML = "你当前使用的浏览器不支持Geolocation服务";
}
</script>
</body>
</html>