Back to Uni App

Compass

docs/api/compass.md

2.3.315.3 KB
Original Source

uni.onCompassChange(callback) @oncompasschange

监听罗盘数据

onCompassChange 兼容性

Web微信小程序AndroidiOSHarmonyOS
4.04.41<a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a>

参数

名称类型必填默认值兼容性描述
callback(result: OnCompassChangeSuccess) => void-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x

OnCompassChangeSuccess 的属性值 @oncompasschangesuccess-values

名称类型必备默认值兼容性描述
directionnumber-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x面对的方向度数
accuracyany-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x需要基础库: 2.4.0

精度 | | errMsg | string | 否 | - | Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x | 错误信息 |

参见

uni.offCompassChange(callback) @offcompasschange

取消监听罗盘数据

offCompassChange 兼容性

Web微信小程序AndroidiOSHarmonyOS
4.04.41<a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a>

参数

名称类型必填默认值兼容性描述
callback(result: OnCompassChangeSuccess) => void-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x-

OnCompassChangeSuccess 的属性值 @oncompasschangesuccess-values

名称类型必备默认值兼容性描述
directionnumber-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x面对的方向度数
accuracyany-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x需要基础库: 2.4.0

精度 | | errMsg | string | 否 | - | Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x | 错误信息 |

参见

uni.startCompass(options?) @startcompass

开始监听罗盘数据

startCompass 兼容性

Web微信小程序AndroidiOSHarmonyOS
4.04.41<a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a>

参数

名称类型必填默认值兼容性描述
optionsStartCompassOptions-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: xuni.startCompass

options 的属性描述

名称类型必备默认值兼容性描述
success(res: CompassSuccess) => void-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: xuni.startCompass成功回调函数定义
fail(res: CompassFail) => void-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: xuni.startCompass失败回调函数定义
complete(res: any) => void-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: xuni.startCompass完成回调函数定义

CompassSuccess 的属性值 @compasssuccess-values

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x错误信息

CompassFail 的属性值 @compassfail-values

名称类型必备默认值兼容性描述
errCodenumber-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x
errMsgstring-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x错误信息
errSubjectstring-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x统一错误主题(模块)名称
dataany-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x错误信息中包含的数据
causeError-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x源错误信息,可以包含多个错误,详见SourceError

参见

uni.stopCompass(options?) @stopcompass

停止监听罗盘数据

stopCompass 兼容性

Web微信小程序AndroidiOSHarmonyOS
4.04.41<a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a><a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a>

参数

名称类型必填默认值兼容性描述
optionsStopCompassOptions-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x-

options 的属性描述

名称类型必备默认值兼容性描述
success(res: CompassSuccess) => void-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x成功返回的回调函数
fail(res: CompassFail) => void-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x失败的回调函数
complete(res: any) => void-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x结束的回调函数(调用成功、失败都会执行)

CompassSuccess 的属性值 @compasssuccess-values

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x错误信息

CompassFail 的属性值 @compassfail-values

名称类型必备默认值兼容性描述
errCodenumber-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x
errMsgstring-Web: -; 微信小程序: 4.41; Android: x; iOS: x; HarmonyOS: x错误信息
errSubjectstring-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x统一错误主题(模块)名称
dataany-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x错误信息中包含的数据
causeError-Web: -; 微信小程序: -; Android: x; iOS: x; HarmonyOS: x源错误信息,可以包含多个错误,详见SourceError

参见

示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/compass/compass

示例

vue
<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>

:::

通用类型

GeneralCallbackResult

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -错误信息