swan.stopCompass

解释:停止监听罗盘数据。

# 方法参数

Object object

# object 参数说明

属性名 类型 必填 默认值 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

# 示例

扫码体验
重新加载
请使用百度APP扫码

# 图片示例

# 代码示例

    <view class="card-area">
        <view class="wrap">
            <view class="page-top">
                <view class="tip-week">{{title}}</view>
            </view>
            <view class="direction">
                <view class="bg-compass-line"></view>
                <view class="bg-compass" style="transform: rotate(-{{direction}}deg)">
                    <view class="num num0" style="transform: rotate({{direction}}deg)">0</view>
                    <view class="num num90" style="transform: rotate({{direction}}deg)">90</view>
                    <view class="num num180" style="transform: rotate({{direction}}deg)">180</view>
                    <view class="num num270" style="transform: rotate({{direction}}deg)">270</view>
                </view>
                <div class="cg-wrap">
                    <div class="circle-left-wrap"><div class="circle-left" style="transform:rotate({{leftdeg}}deg);transform-origin:50% 50%"></div></div>
                    <div class="circle-right-wrap"><div class="circle-right" style="transform:rotate({{rightdeg}}deg);transform-origin:50% 50%"></div></div>
                    <div class="mask">
                        <view class="direction-num">
                            <text>{{direction}}</text>
                            <text class="direction-degree"></text>
                        </view>
                    </div>
                </div>
            </view>
            <view class="page-body-btn">
                <view class="middle-btn {{!isWatch ? '' : 'disabled'}}" bind:tap="startWatch">开始监听</view>
                <view class="middle-btn {{!isWatch ? 'disabled' : ''}}" bind:tap="stopWatch">停止监听</view >
            </view>
        </view>
    </view>
    
    Page({
        data: {
            title: '旋转手机即可获取方位信息',
            direction: 0,
            isWatch: true,
            leftdeg: 0,
            rightdeg: 0
        },
    
        onReady() {
            swan.onCompassChange(res => {
                this.setData({
                    direction: parseInt(res.direction, 10)
                });
                this.updateProgress(res.direction);
            });
        },
        onShow() {
            let isWatch = this.data.isWatch;
            isWatch ? swan.startCompass() : swan.stopCompass();
        },
        startWatch() {
            this.setData('isWatch', true);
            swan.startCompass();
        },
        stopWatch() {
            this.setData('isWatch', false);
            swan.stopCompass();
        },
        updateProgress(direction) {
            let leftdeg;
            let rightdeg;
            let deg = +direction;
            if (deg > 180) {
                leftdeg = 180;
                rightdeg = 180 - deg;
            }
            else {
                leftdeg = -deg;
                rightdeg = 0;
            }
            this.setData({
                leftdeg,
                rightdeg
            });
        }
    });