HTML5学习(七)----地理定位

参考教程:http://www.w3school.com.cn/html5/html_5_geolocation.asphtml

 说明:设备必须有GPS定位功能才能定位的html5

 

定位用户的位置

HTML5 Geolocation API 用于得到用户的地理位置。git

 

 

<!DOCTYPE html>
<html>
<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>说明:html5的定位还比较准,我在Android与iphone上都有测试过,另外这种定位方式也比较简单
相关文章
相关标签/搜索