js手机获取当前经纬度

<!DOCTYPE html>
<html>
  <head>
    
  </head>
<body>
<p id="demo">点击这个按钮,获得您的坐标:</p>
<button onclick="getLocation()">试一下</button>
<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>

该方法必须在手机上测试才有有效

posted @ 2017-11-11 16:29  守y1座_空城  阅读(6923)  评论(1编辑  收藏  举报