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

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

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

 

定位用户的位置

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

 

 

<!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上都有测试过,另外这种定位方式也比较简单

 

你可能感兴趣的:(html5)