zoukankan      html  css  js  c++  java
  • 【H5】 经纬度位置获取navigator.geolocation.getCurrentPosition

    navigator.geolocation.getCurrentPosition(function(){
    })
    经度 : coords.longitude

    纬度 : coords.latitude

    准确度 : coords.accuracy

    海拔 : coords.altitude

    海拔准确度 : coords.altitudeAcuracy

    行进方向 : coords.heading

    地面速度 : coords.speed

    请求的时间: new Date(position.timestamp)

    获取方法代码如下:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
      <style>
        * { margin: 0; padding: 0;}
        #box {
           500px;
          height: 500px;
          border: 2px solid deeppink;
        }
      </style>
    </head>
    
    <body>
      <button id='btn'> 请求位置信息 </button>
      <div id="box"></div>
    
      <script>
        let btn = document.getElementById('btn');
        let box = document.getElementById('box');
    
        //点击按钮获取地理位置信息
        btn.onclick = function () {
          //getCurrentPosition与定时器setInterval类似多次请求,因为位置需要不间断的获取
          //直接navigator.geolocation表示单次获取位置
          navigator.geolocation.getCurrentPosition(function (position) {
            box.innerHTML += "经度" + position.coords.longitude;
            box.innerHTML += "纬度" + position.coords.latitude;
            box.innerHTML += "准确度" + position.coords.accuracy;
            box.innerHTML += "海拔" + position.coords.altitude;
            box.innerHTML += "海拔准确度" + position.coords.altitudeAcuracy;
            box.innerHTML += "行进方向" + position.coords.heading;
            box.innerHTML += "地面速度" + position.coords.speed;
            box.innerHTML += "请求的时间" + new Date(position.timestamp);
          }, function (err) {
            alert(err.code);
    // code:返回获取位置的状态
    //          0  :  不包括其他错误编号中的错误
    // ​		     1  :  用户拒绝浏览器获取位置信息
    // ​		     2  :  尝试获取用户信息,但失败了
    // ​		     3  :   设置了timeout值,获取位置超时了
          }, {
              enableHighAcuracy: false, //位置是否精确获取
              timeout: 5000,            //获取位置允许的最长时间
              maximumAge: 1000          //多久更新获取一次位置
            })
        }
      </script>
    </body>
    </html>
    

      

    IE浏览器运行结果如下:

    谷歌浏览器限制了获取

  • 相关阅读:
    N层电梯只停一层情况下,求所有人爬楼层数最少
    小组开发用户调研
    《哈利波特》买书最优惠算法
    团队开发——极速蜗牛
    林锐——软件工程思想后两章阅读笔记
    课堂练习之检测水军
    团队开发项目-----来用------典型用户与用户场景分析
    体验结对开发的乐趣(6)--(电梯调度问题)
    团队开发项目-----来用------用户需求调研报告
    课堂练习之最高折扣,最低优惠规划
  • 原文地址:https://www.cnblogs.com/laneyfu/p/11922675.html
Copyright © 2011-2022 走看看