zoukankan      html  css  js  c++  java
  • h5之获取朝向和定位

    h5之获取朝向和定位

    定位:

    通过h5中的getCurrentPosition()方法可以获取到移动设备定位的经纬度

        function getLocation() {
            var that = this
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    alert('经度:'+ position.coords.latitude)
                    alert('纬度:'+ position.coords.longitude)
                }, function (error) {
                    console.log(error)
                    switch (error.code) {
                        case error.PERMISSION_DENIED:
                            alert('用户拒绝对获取地理位置的请求。')
                            break;
                        case error.POSITION_UNAVAILABLE:
                            alert('位置信息是不可用的。')
                            break;
                        case error.TIMEOUT:
                            alert('请求用户地理位置超时。')
                            break;
                        case error.UNKNOWN_ERROR:
                            alert('未知错误。')
                            break;
                    }
                }, { enableHighAcuracy: false });
            } else {
                alert('Geolocation is not supported by this browser.')
            }
        }

    在getCurrentPosition()的api中可以获取到heading属性,其为手机的朝向。但经过测试,获取到的朝向值为null或者0.

    所以朝向需要通过另一种方法获取。

    朝向:

    朝向的获取可以通过监听手机陀螺仪的数据变化获取:

        window.addEventListener('deviceorientation', function(e){
            console.log('absolute: ' + e.absolute)
            console.log('alpha: ' + e.alpha)
            console.log('beta: ' + e.beta)
            console.log('gamma: ' + e.gamma)
        }, false);

    其中alpha值,即为水平方向上的手机朝向。

    钻研不易,转载请注明出处......

  • 相关阅读:
    六、order set结构及命令详解
    五、set结构及命令详解
    四、redis的link结构及命令详解
    三、redis对字符串类型的操作
    二、redis对于key的操作命令
    一、redis的特点以及安装使用
    Mysql5.7以上版本group by报错问题
    1.4 java高并发程序设计-无锁
    sysbench工具和mysql的基准测试
    sqli-labs(29-31关)
  • 原文地址:https://www.cnblogs.com/s313139232/p/13074083.html
Copyright © 2011-2022 走看看