首页 > 代码库 > H5 获取地理位置

H5 获取地理位置

只能通过手机浏览器访问,并且用户必须允许访问才可以生效

<!doctype html>
<html>
  <head>
        <title>Mobile Cookbook</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <header>
    </header>
        <div id="main">
            <div id="someElm">
            </div>
        </div>
    <footer>
    </footer>
    <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
    <script>
    function getLocation() {
    navigator.geolocation.getCurrentPosition(showInfo);
  }
  function showInfo(position) {
      console.log(position);
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var accuracy = position.coords.accuracy;
    $(#someElm).html(latitude: +latitude+<br />longitude: +longitude+<br />accuracy: +accuracy);
  }
  getLocation();
    </script>
    </body>
</html>

技术分享

 

H5 获取地理位置