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浏览器运行结果如下:

    谷歌浏览器限制了获取

  • 相关阅读:
    结对编程2—单元测试
    个人作业2—英语学习APP案例分析
    结对作业--基于GUI的四则运算生成器
    基于控制台的四则运算
    关于软件工程这门课
    个人作业3——个人总结(Alpha阶段)
    结对编程2——单元测试
    个人作业2——英语学习APP案例分析
    结对作业--基于GUI的四则运算生成器
    个人作业一
  • 原文地址:https://www.cnblogs.com/laneyfu/p/11922675.html
Copyright © 2011-2022 走看看