更新记录
1.0.2(2025-06-13)
1.0.1(2025-06-12)
- 【新增】安卓:申请权限,单次定位,连续定位,场景定位,后台定位
- 【新增】使用文档教程
1.0.0(2025-06-11)
查看更多
平台兼容性
uni-app(4.66)
Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
iOS |
鸿蒙 |
√ |
- |
- |
- |
- |
- |
5.0 |
- |
- |
微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
快应用-华为 |
快应用-联盟 |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
uni-app x(4.63)
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.county}}</t-text>
<t-text :ma="[5,0]">街道:{{data.town}}</t-text>
<t-text :ma="[5,0]">城市码:{{data.cityCode}}</t-text>
</t-card>
<t-card title="接口">
<t-button :ma="[10,0]" type="primary" @click="requestPermission">申请权限</t-button>
<t-button :ma="[10,0]" type="primary" @click="getOnceLocation">开启单次定位</t-button>
<t-button :ma="[10,0]" type="primary" @click="getContinuationLocation">开启连续定位</t-button>
<t-button :ma="[10,0]" type="primary" @click="stopContinuationLocation">停止连续定位</t-button>
<t-button :ma="[10,0]" type="primary" @click="getSceneLocation">开启场景定位</t-button>
<t-button :ma="[10,0]" type="primary" @click="stopSceneLocation">停止场景定位</t-button>
<t-button :ma="[10,0]" type="primary" @click="getForegroundLocation">开启后台定位</t-button>
<t-button :ma="[10,0]" type="primary" @click="stopForegroundLocation">关闭后台定位</t-button>
</t-card>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
// #ifdef APP
import { TTecentLocationApi,TLocationOptions,TLocationResult,TLocationData } from "@/uni_modules/t-tlocation-api"
// #endif
import { reactive } from 'vue'
type stateType = {
loading: boolean;
}
// #ifdef APP
const tLocation = new TTecentLocationApi()
// #endif
const state = reactive<stateType>({
loading: false
})
let data = ref<TLocationData>({
address: "",
city: "",
cityCode: "",
country: "",
county: "",
latitude: 0,
longitude: 0,
province: "",
town: "",
})
const requestPermission = () => {
// #ifdef APP
tLocation.requestPermission({
success: (result: TLocationResult) => {
console.log(result)
},
fail: (result: TLocationResult) => {
console.log(result)
}
})
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
const getOnceLocation = () => {
try {
// #ifdef APP
tLocation.getOnceLocation({
success: (result: TLocationResult) => {
console.log(result,result.data?.address)
data.value.address = result.data?.address
data.value.city = result.data?.city
data.value.cityCode = result.data?.cityCode
data.value.country = result.data?.country
data.value.county = result.data?.county
data.value.latitude = result.data?.latitude
data.value.longitude = result.data?.longitude
data.value.province = result.data?.province
data.value.town = result.data?.town
},
fail: (result: TLocationResult) => {
console.log(result)
}
} as TLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
} catch (error) {
//TODO handle the exception
console.log(error)
}
}
const getContinuationLocation = () => {
// #ifdef APP
tLocation.getContinuationLocation({
success: (result: TLocationResult) => {
console.log(result)
data.value.address = result.data?.address
data.value.city = result.data?.city
data.value.cityCode = result.data?.cityCode
data.value.country = result.data?.country
data.value.county = result.data?.county
data.value.latitude = result.data?.latitude
data.value.longitude = result.data?.longitude
data.value.province = result.data?.province
data.value.town = result.data?.town
},
fail: (result: TLocationResult) => {
console.log(result)
}
} as TLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
const stopContinuationLocation = () => {
// #ifdef APP
tLocation.stopContinuationLocation({
success: (result: TLocationResult) => {
console.log(result)
data.value.address = result.data?.address
data.value.city = result.data?.city
data.value.cityCode = result.data?.cityCode
data.value.country = result.data?.country
data.value.county = result.data?.county
data.value.latitude = result.data?.latitude
data.value.longitude = result.data?.longitude
data.value.province = result.data?.province
data.value.town = result.data?.town
},
fail: (result: TLocationResult) => {
console.log(result)
}
} as TLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
const getSceneLocation = () => {
// #ifdef APP
tLocation.getSceneLocation({
success: (result: TLocationResult) => {
console.log(result)
data.value.address = result.data?.address
data.value.city = result.data?.city
data.value.cityCode = result.data?.cityCode
data.value.country = result.data?.country
data.value.county = result.data?.county
data.value.latitude = result.data?.latitude
data.value.longitude = result.data?.longitude
data.value.province = result.data?.province
data.value.town = result.data?.town
},
fail: (result: TLocationResult) => {
console.log(result)
}
} as TLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
const stopSceneLocation = () => {
// #ifdef APP
tLocation.stopSceneLocation({
success: (result: TLocationResult) => {
console.log(result)
},
fail: (result: TLocationResult) => {
console.log(result)
}
} as TLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
const getForegroundLocation = () => {
// #ifdef APP
tLocation.getForegroundLocation({
success: (result: TLocationResult) => {
console.log(result)
data.value.address = result.data?.address
data.value.city = result.data?.city
data.value.cityCode = result.data?.cityCode
data.value.country = result.data?.country
data.value.county = result.data?.county
data.value.latitude = result.data?.latitude
data.value.longitude = result.data?.longitude
data.value.province = result.data?.province
data.value.town = result.data?.town
},
fail: (result: TLocationResult) => {
console.log(result)
}
} as TLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
const stopForegroundLocation = () => {
// #ifdef APP
tLocation.stopForegroundLocation({
success: (result: TLocationResult) => {
console.log(result)
},
fail: (result: TLocationResult) => {
console.log(result)
}
} as TLocationOptions)
// #endif
// #ifdef WEB
uni.showToast({
title: "请在App端访问",
icon: "none"
})
// #endif
}
onReady(() => {
state.loading = true
tLocation.setUserAgreePrivacy()
tLocation.setKey("你的key")
})
</script>
暴露的类型
/**
* @property {String} address 地址信息
* @property {String} city 城市
* @property {String} cityCode 城市码
* @property {String} country 国家
* @property {String} county 区域
* @property {String} latitude 纬度
* @property {String} longitude 经度
* @property {String} province 省
* @property {String} town 街道
*/
export type TLocationData = {
address?: string;
city?: string;
cityCode?: string;
country?: string;
county?: string;
latitude?: number;
longitude?: number;
province?: string;
town?: string;
}
/**
* @property {Number} code 状态码
* @value 0 成功
* @value -1 失败或系统错误
* @value 其他参考文档:https://lbs.qq.com/mobile/androidLocationSDK/androidGeoGuide/androidGeoError
* @property {String} msg 状态消息
* @property {TLocationData} data 成功的数据
*/
export type TLocationResult = {
code: number;
msg: string;
data?: TLocationData
}
/**
* @property {Number} interval 定位周期(位置监听器回调周期),默认值:3000
* @property {Boolean} allowGps 是否允许GPS,默认值:true
* @property {Number} requestLevel 位置信息的详细程度,默认值:1
* @value 0 包含经纬度
* @value 1 包含经纬度, 位置名称, 位置地址
* @value 3 包含经纬度,位置所处的中国大陆行政区划
* @value 4 包含经纬度,位置所处的中国大陆行政区划及周边POI列表
* @property {Boolean} allowDirection 是否需要获取传感器方向,默认值:fasle
* @property {Boolean} indoorLocationMode 是否需要开启室内定位,默认值:true
* @property {Number} locMode 定位模式,默认值:1
* @value 1 高精度定位模式
* @value 2 仅网络定位模式
* @value 3 仅GPS定位模式
* @property {Number} sceneMode 场景模式
* @value 1 签到场景,单次定位,第一次会优先返回精度较高的结果,定位速度可能会延迟1~3S
* @value 2 运动场景,高精度连续定位,适用于步行或骑行定位,第一次会优先返回精度较高的结果,定位可能会延迟3~5s
* @value 3 出行场景,高精度连续定位,适用于室外出行场景,优先使用卫星定位结果,卫星定位成功之后网络定位不再返回,卫星信号断开之后一段时间才会返回网络结果,超时时间为8s
* @property {Number} gnssSource 卫星来源
* @property {String} appName App名称,用于后台定位显示通知
*/
export type TLocationOptions = {
appName?: string;
interval?: number;
allowGps?: boolean;
requestLevel?: number;
allowDirection?: boolean;
indoorLocationMode?: boolean;
locMode?: number;
gpsFirst?: boolean;
sceneMode?: number;
gnssSource?: number;
success?: (result: TLocationResult) => void;
fail?: (result: TLocationResult) => void;
}