html5获取当前手机位置的精度维度

时间:2021-02-28 10:51:39

布局代码如下

<p id="demo">点击这个按钮,获得您的坐标:</p>
<button onclick="getLocation()">试一下</button>

js代码如下

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;
}