# uni.openLocation(options)

注意

  • HBuilderX 5.11 起,openLocation 已内置在 uni-app x 框架内,无需再单独导入 uni-openlocation 插件;

  • HBuilderX 5.11 之前,如需使用,请前往插件市场导入该插件:插件地址

  • 无论是内置能力还是导入 uni-openlocation 插件,通过 uni.openLocation 打开的页面都会以 dialogPage 的形式呈现。此时执行 getDialogPages 方法时,由 openLocation 打开的页面也会包含在返回的页面数组中,这属于正常现象。

  • HarmonyOS平台 调用此 API 需要申请定位权限 ohos.permission.APPROXIMATELY_LOCATIONohos.permission.LOCATION,需自行在项目中配置权限。

使用应用内置地图查看位置

# openLocation 兼容性

Web 微信小程序 Android iOS HarmonyOS
4.0 5.11 5.11 5.11 5.11

# 参数

名称 类型 必填 默认值 兼容性 描述
options OpenLocationOptions - - uni.openLocation函数参数定义
名称 类型 必备 默认值 兼容性 描述
latitude number -
纬度,范围为-90~90,负数表示南纬,使用 gcj02 国测局坐标系
longitude number -
经度,范围为-180~180,负数表示西经,使用 gcj02 国测局坐标系
scale number -
缩放比例,范围5~18,默认为18(微信小程序)
name string -
位置名
address string -
地址的详细说明
success (result: OpenLocationSuccess) => void -
接口调用成功的回调函数
fail (result: OpenLocationFail) => void -
接口调用失败的回调函数
complete (result: any) => void -
接口调用结束的回调函数(调用成功、失败都会执行)

# OpenLocationSuccess 的属性值

名称 类型 必备 默认值 兼容性 描述
errMsg string - - -

# OpenLocationFail 的属性值

名称 类型 必备 默认值 兼容性 描述
errCode number - - 错误码
errSubject string - - 统一错误主题(模块)名称
data any - - 错误信息中包含的数据
cause Error - - 源错误信息,可以包含多个错误,详见SourceError
errMsg string - - -

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

扫码体验(手机浏览器跳转到App直达页)

示例

<template>
  <view>
    <page-head :title="title"></page-head>
    <view class="uni-common-mt">
      <view class="uni-list">
        <view class="uni-list-cell">
          <view class="uni-list-cell-left">
            <view class="uni-label">经度</view>
          </view>
          <view class="uni-list-cell-db">
            <text class="readonly-text">{{ longitude }}</text>
          </view>
        </view>
        <view class="uni-list-cell">
          <view class="uni-list-cell-left">
            <view class="uni-label">纬度</view>
          </view>
          <view class="uni-list-cell-db">
            <text class="readonly-text">{{ latitude }}</text>
          </view>
        </view>
        <view class="uni-list-cell">
          <view class="uni-list-cell-left">
            <view class="uni-label">位置名称</view>
          </view>
          <view class="uni-list-cell-db">
            <text class="readonly-text">{{ name }}</text>
          </view>
        </view>
        <view class="uni-list-cell">
          <view class="uni-list-cell-left">
            <view class="uni-label">详细位置</view>
          </view>
          <view class="uni-list-cell-db">
            <text class="readonly-text">{{ address }}</text>
          </view>
        </view>
      </view>
      <view class="uni-padding-wrap">
        <view class="tips">注意:需要正确配置地图服务商的Key才能正常显示位置</view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @click="openLocation">查看位置</button>
        </view>
      </view>
    </view>
  </view>
</template>
<script lang="uts" setup>
  import { state, setLifeCycleNum } from '@/store/index.uts';

  type DataType = {
    dialogPagesNum: number;
  };

  // 响应式数据
  const title = ref('openLocation');
  const longitude = ref(116.39747);
  const latitude = ref(39.9085);
  const name = ref('天安门');
  const address = ref('北京市东城区东长安街');
  // 自动化测试
  const data = reactive({
    dialogPagesNum: -1,
  } as DataType);

  // 生命周期钩子
  onPageShow(() => {
    console.log('Page Show');
    // 自动化测试
    setLifeCycleNum(state.lifeCycleNum + 1);
  });

  onPageHide(() => {
    console.log('Page Hide');
    // 自动化测试
    setLifeCycleNum(state.lifeCycleNum - 1);
  });

  // 自动化测试
  const test = () => {
    const pages = getCurrentPages();
    const page = pages[pages.length - 1];
    // #ifdef APP || WEB
    const dialogPages = page.getDialogPages();
    data.dialogPagesNum = dialogPages.length;
    // #endif
  };

  // 方法
  const openLocation = () => {
    uni.openLocation({
      longitude: longitude.value,
      latitude: latitude.value,
      name: name.value,
      address: address.value,
    });
    // 自动化测试
    setTimeout(() => {
      test();
    }, 500);
  };

  // 自动化测试
  const pageSetLifeCycleNum = (value: number) => {
    setLifeCycleNum(value);
  };

  // 自动化测试
  const getLifeCycleNum = (): number => {
    return state.lifeCycleNum;
  };

  defineExpose({
    data,
    openLocation,
    pageSetLifeCycleNum,
    getLifeCycleNum,
  });
</script>

<style>
  .readonly-text {
    color: #999999;
    font-size: 14px;
    line-height: 22px;
    padding-top: 10px;
    padding-bottom: 10px;
  }

  .uni-list-cell-left {
    padding: 0 15px;
  }

  .tips {
    font-size: 12px;
    margin-top: 15px;
    opacity: 0.8;
  }
</style>

# 参见