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