监听罗盘数据变化事件。频率:5 次/秒,接口调用后会自动开始监听
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
App平台另见 https://gitcode.com/dcloud/utsPlugIn/
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| callback | (result: OnCompassChangeSuccess) => void | 是 | - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| direction | number | 是 | - | - | |
| accuracy | number | string | 否 | - | - | |
| errMsg | string | 否 | - | - |
取消监听罗盘数据
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
App平台另见 https://gitcode.com/dcloud/utsPlugIn/
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| callback | (result: OnCompassChangeSuccess) => void | 是 | - | - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| direction | number | 是 | - | - | |
| accuracy | number | string | 否 | - | - | |
| errMsg | string | 否 | - | - |
开始监听罗盘数据
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
App平台另见 https://gitcode.com/dcloud/utsPlugIn/
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | StartCompassOptions | 否 | - | - | |||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 否 | - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errCode | number | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errSubject | string | 是 | - | - | 统一错误主题(模块)名称 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data | any | 否 | - | - | 错误信息中包含的数据 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errMsg | string | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| interface | - | - |
| interface | - | - |
停止监听罗盘数据
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
App平台另见 https://gitcode.com/dcloud/utsPlugIn/
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | StopCompassOptions | 否 | - | - | |||||||||||||||||||||||||
| |||||||||||||||||||||||||||||
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 否 | - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errCode | number | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errSubject | string | 是 | - | - | 统一错误主题(模块)名称 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data | any | 否 | - | - | 错误信息中包含的数据 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errMsg | string | 是 | - | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| interface | - | - |
| interface | - | - |
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
示例
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap">
<view class="uni-hello-text uni-center" style="padding-bottom:25px;">
旋转手机即可获取方位信息
</view>
<view class="direction">
<view class="bg-compass-line"></view>
<image class="bg-compass" src="../../../static/compass.png" :style="'transform: rotate('+direction+'deg)'">
</image>
<view class="direction-value">
<text>{{direction}}</text>
<text class="direction-degree">o</text>
</view>
</view>
</view>
</view>
</template>
<script setup lang="uts">
const title = ref('onCompassChange')
const direction = ref(0)
onReady(() => {
uni.onCompassChange((res) => {
console.log('onCompassChange', res)
direction.value = res.direction
})
})
onUnload(() => {
uni.stopCompass();
direction.value = 0;
})
</script>
<style>
.direction {
position: relative;
margin-top: 35px;
display: flex;
width: 270px;
height: 270px;
align-items: center;
justify-content: center;
margin: 0 auto;
}
.direction-value {
position: relative;
font-size: 100px;
color: #353535;
line-height: 1;
z-index: 1;
}
.direction-degree {
position: absolute;
top: 0;
right: -20px;
font-size: 30px;
}
.bg-compass {
position: absolute;
top: 0;
left: 0;
width: 270px;
height: 270px;
transition: .1s;
}
.bg-compass-line {
position: absolute;
left: 134px;
top: -5px;
width: 3px;
height: 28px;
background-color: #1AAD19;
border-radius: 100px;
z-index: 1;
}
</style>
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | 错误信息 |