//直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>获取位置</title>
</head>
<body>
<p>你的坐标是:</p>
<button onclick="show()">获取位置</button>
<div></div>
<script>
var x = document.querySelector('div')
function show(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition)
}else{
x.innerHTML='你的浏览器不支持地理位置'
}
}
function showPosition(obj){
x.innerHTML= '纬度:'+obj.coords.latitude+"<br/>经度:"+obj.coords.longitude
}
</script>
</body>
</html>
你的坐标是:
获取位置