更新记录
1.0.3(2025-06-13)
- 【新增】场景定位
- 【新增】文档教程
- 【优化】定位写法,增加定位字段
平台兼容性
uni-app(3.1.0)
Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
iOS |
鸿蒙 |
√ |
- |
- |
- |
- |
- |
5.0 |
- |
- |
微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
快应用-华为 |
快应用-联盟 |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
uni-app x(3.1.0)
Chrome |
Safari |
Android |
iOS |
鸿蒙 |
微信小程序 |
- |
- |
5.0 |
- |
- |
- |
Turbo UI 系列插件 - 百度定位SDK
- 如您已购买
Turbo UI
,那么该插件可以免费使用源码版!
- 使用需要打自定义基座,最新支持4.71,4.66,4.63版本,其他自测
- 支持基础定位,场景定位
使用示例
<template>
<t-navbar title="百度定位" :is-back="true"></t-navbar>
<!-- #ifdef APP -->
<scroll-view style="flex: 1;">
<!-- #endif -->
<t-card title="定位信息">
<t-text :ma="[5,0]">经度:{{data.longitude}}</t-text>
<t-text :ma="[5,0]">纬度:{{data.latitude}}</t-text>
<t-text :ma="[5,0]">地址信息:{{data.address}}</t-text>
<t-text :ma="[5,0]">国家:{{data.country}}</t-text>
<t-text :ma="[5,0]">省:{{data.province}}</t-text>
<t-text :ma="[5,0]">市:{{data.city}}</t-text>
<t-text :ma="[5,0]">区:{{data.district}}</t-text>
<t-text :ma="[5,0]">街道:{{data.street}}</t-text>
<t-text :ma="[5,0]">乡镇:{{data.town}}</t-text>
</t-card>
<t-card title="接口">
<t-button :ma="[10,0]" type="primary" @click="startLocation">开启定位</t-button>
<t-button :ma="[10,0]" type="primary" @click="stopLocation">停止定位</t-button>
<t-button :ma="[10,0]" type="primary" @click="getSceneLocation">开启场景定位</t-button>
</t-card>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
// #ifdef APP
import { TBaiduLocationApi,TBaiduLocationOptions,TBaiduLocationResult,TBaiduLocationData } from "@/uni_modules/t-baidu-location"
// #endif
import { reactive } from 'vue'
type stateType = {
loading: boolean;
}
// #ifdef APP
const tBaiduLocationApi = new TBaiduLocationApi()
// #endif
const state = reactive<stateType>({
loading: false
})
const data = ref<TBaiduLocationData>({
})
const startLocation = () => {
// #ifdef APP
tBaiduLocationApi.getLocation({
success: (result: TBaiduLocationResult) => {
data.value.address = result.data?.address
data.value.latitude = result.data?.latitude
data.value.longitude = result.data?.longitude
data.value.country = result.data?.country
data.value.province = result.data?.province
data.value.city = result.data?.city
data.value.district = result.data?.district
data.value.street = result.data?.street
data.value.adcode = result.data?.adcode
data.value.town = result.data?.town
},
fail: (result: TBaiduLocationResult) => {
console.log(result)
}
} as TBaiduLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
const stopLocation = () => {
// #ifdef APP
tBaiduLocationApi.stopLocation({
success: (result: TBaiduLocationResult) => {
console.log(result)
},
fail: (result: TBaiduLocationResult) => {
console.log(result)
}
} as TBaiduLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
const getSceneLocation =() => {
// #ifdef APP
tBaiduLocationApi.getSceneLocation({
scene: 1,
success: (result: TBaiduLocationResult) => {
console.log(result)
data.value.address = result.data?.address
data.value.latitude = result.data?.latitude
data.value.longitude = result.data?.longitude
data.value.country = result.data?.country
data.value.province = result.data?.province
data.value.city = result.data?.city
data.value.district = result.data?.district
data.value.street = result.data?.street
data.value.adcode = result.data?.adcode
data.value.town = result.data?.town
},
fail: (result: TBaiduLocationResult) => {
console.log(result)
}
} as TBaiduLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
onReady(() => {
state.loading = true
// #ifdef APP
TBaiduLocationApi.setKey("你的AK")
// #endif
})
</script>
暴露的类型
/**
* @property {Number} latitude 纬度
* @property {Number} longitude 经度
* @property {String} address 地址
* @property {String} country 国家
* @property {String} province 省份
* @property {String} city 城市
* @property {String} district 区县
* @property {String} street 街道信息
* @property {String} adcode adcode
* @property {String} town 乡镇信息
*/
export type TBaiduLocationData = {
latitude?: number;
longitude?: number;
address?: string;
country?: string;
province?: string;
city?: string;
district?: string;
street?: string;
adcode?: string;
town?: string;
}
/**
* @description 返回信息
* @property {Number} code 状态码
* @property {String} msg 状态消息
* @property {TBaiduLocationData} data 定位数据
*/
export type TBaiduLocationResult = {
code: number;
msg: string;
data?: TBaiduLocationData;
}
/**
* @property {Number} mode 定位模式
* @value 1 高精度
* @value 2 低功耗
* @value 3 仅使用设备
* @value 4 模糊定位模式
* @property {String} coorType 经纬度坐标类型
* @value gcj02 国测局坐标
* @value bd09ll 百度经纬度坐标
* @value bd09 百度墨卡托坐标
* @property {Boolean} needAddress 是否需要地址信息
* @property {Boolean} needNewVersionRgc 设置是否需要最新版本的地址信息
* @property {Boolean} needLocationDescribe 是否需要位置描述信息
* @property {Boolean} needLocationPoiList 是否需要周边POI信息
* @property {Number} scene 场景模式,仅场景定位有效
* @value 1 签到场景
* @value 2 运动场景
* @value 3 出行场景
*/
export type TBaiduLocationOptions = {
mode?: number;
coorType?: "gcj02" | "bd09ll" | "bd09";
needAddress?: boolean;
needNewVersionRgc?: boolean;
needLocationDescribe?: boolean;
needLocationPoiList?: boolean;
scene?: number;
success?: (result: TBaiduLocationResult) => void;
fail?: (result: TBaiduLocationResult) => void;
}