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值,即为水平方向上的手机朝向。

 

 

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM