codecamp

百度智能小程序 监听设备方向变化

swan.onDeviceMotionChange

基础库 3.30.2 开始支持,低版本需做兼容处理。在工具和真机中的实现有区别,详见 API 实现差异

解释:监听设备方向变化事件。频率根据 swan.startDeviceMotionListening() 的 interval 参数。可以使用 swan.stopDeviceMotionListening() 停止监听。

方法参数

Function callback

callback参数说明

属性名类型必填默认值说明

alpha

Number

当手机坐标 X/Y 和 地球 X/Y 重合时,绕着 Z 轴转动的夹角为 alpha,范围值为 [0, 2*PI)。逆时针转动为正。

beta

Number

当手机坐标 Y/Z 和地球 Y/Z 重合时,绕着 X 轴转动的夹角为 beta。范围值为 [-1*PI, PI) 。顶部朝着地球表面转动为正。也有可能朝着用户为正。

gamma

Number

当手机 X/Z 和地球 X/Z 重合时,绕着 Y 轴转动的夹角为 gamma。范围值为 [-1*PI/2, PI/2)。右边朝着地球表面转动为正。

示例 

在开发者工具中打开


图片示例

代码示例

<view class="wrap">
    <view class="card-area">
        <view class="list-area border-bottom" s-for="item in infoList">
            <text class="list-item-key-4">{{item.chineseName}}</text>
            <text class="list-item-value">{{item.value}}</text>
        </view>
        <view class="button-group">
            <button bind:tap="stopDeviceMotionListening" disabled="{{!disabled}}" type="primary" hover-stop-propagation="true">停止监听</button>
            <button bind:tap="startDeviceMotionListening" disabled="{{disabled}}" type="primary" hover-stop-propagation="true">开始监听</button>
        </view>
    </view>
</view>
Page({
    data: {
        infoList: [{
            engName: 'alpha',
            chineseName: 'z轴角度',
            value: ''
        }, {
            engName: 'beta',
            chineseName: 'x轴角度',
            value: ''
        }, {
            engName: 'gamma',
            chineseName: 'y轴角度',
            value: ''
        }],
        disabled: false
    },
    startDeviceMotionListening() {
        swan.startDeviceMotionListening({
            interval: 'ui',
            success: () => {
                this.setData('disabled', !this.data.disabled);
                console.log('startDeviceMotionListening success');
                this.onDeviceMotionChange();
            },
            fail: err => {
                console.log('startDeviceMotionListening fail', err);
            }
        });
    },
    onDeviceMotionChange() {
        swan.onDeviceMotionChange(res => {
            console.log('onDeviceMotionChange', res);
            this.updateInfoList(res);
        });
    },
    stopDeviceMotionListening() {
        swan.stopDeviceMotionListening({
            success: () => {
                this.setData('disabled', !this.data.disabled);
                console.log('stopDeviceMotionListening success');
            },
            fail: err => {
                console.log('stopDeviceMotionListening fail', err);
            }
        });
    },
    updateInfoList(res) {
        let infoList = this.data.infoList;
        for (let item of infoList) {
            if (!res[item.engName]) {
                item.value = '暂无';
            } else {
                item.value = res[item.engName].toFixed(2);
            }
        }
        this.setData('infoList', infoList);
    }
});


百度智能小程序 停止监听罗盘数据
百度智能小程序 开始监听设备方向变化
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }