更新记录
1.0.3(2025-07-18)
1.0.2(2025-06-13)
1.0.1(2025-06-12)
- 【新增】安卓:申请权限,单次定位,连续定位,场景定位,后台定位
- 【新增】使用文档教程
查看更多
平台兼容性
uni-app x(4.66)
Chrome |
Safari |
Android |
Android插件版本 |
iOS |
鸿蒙 |
微信小程序 |
- |
- |
5.0 |
1.0.3 |
- |
- |
- |
Turbo UI 系列插件 - 腾讯定位SDK
- 如您已购买
Turbo UI
,那么该插件可以免费使用源码版!
- 使用需要打自定义基座,最新支持4.71,4.66,4.63版本,其他自测
- 支持单次定位,持续定位,场景定位,后台定位(基本保活不死)
重要初始化步骤!!!
因为代码内部做了处理所以无需设置AndroidManifest.xml
和Info.plist
- 先调用
requestPermission
- 然后调用
setUserAgreePrivacy
- 最后调用
setKey
如果iOS不生效请修改插件内部Info.plist
,找到TencentLBSAPIKey
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>TencentLBSAPIKey</key>
<string>你的KEY</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>用于提供精确的定位</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>用于提供精确的定位</string>
</dict>
</plist>
使用示例
<script setup lang="uts">
import * as tLocation from "@/uni_modules/t-tlocation-api"
let data = ref<tLocation.TLocationData>({
address: "",
city: "",
cityCode: "",
country: "",
county: "",
latitude: 0,
longitude: 0,
province: "",
town: "",
})
const requestPermission = () => {
tLocation.requestPermission({
success: (result: tLocation.TLocationResult) => {
console.log(result)
},
fail: (result: tLocation.TLocationResult) => {
console.log(result)
}
})
}
const getOnceLocation = () => {
try {
tLocation.getOnceLocation({
success: (result: tLocation.TLocationResult) => {
console.log(result)
},
fail: (result: tLocation.TLocationResult) => {
console.log(result)
}
} as tLocation.TLocationOptions)
} catch (error) {
//TODO handle the exception
console.log(error)
}
}
const getContinuationLocation = () => {
tLocation.getContinuationLocation({
success: (result: tLocation.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: tLocation.TLocationResult) => {
console.log(result)
}
} as tLocation.TLocationOptions)
}
const stopContinuationLocation = () => {
tLocation.stopContinuationLocation({
success: (result: tLocation.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: tLocation.TLocationResult) => {
console.log(result)
}
} as tLocation.TLocationOptions)
}
const getSceneLocation = () => {
tLocation.getSceneLocation({
success: (result: tLocation.TLocationResult) => {
console.log(result)
},
fail: (result: tLocation.TLocationResult) => {
console.log(result)
}
} as tLocation.TLocationOptions)
}
const stopSceneLocation = () => {
tLocation.stopSceneLocation({
success: (result: tLocation.TLocationResult) => {
console.log(result)
},
fail: (result: tLocation.TLocationResult) => {
console.log(result)
}
} as tLocation.TLocationOptions)
}
const getForegroundLocation = () => {
tLocation.getForegroundLocation({
success: (result: tLocation.TLocationResult) => {
console.log(result)
},
fail: (result: tLocation.TLocationResult) => {
console.log(result)
}
} as tLocation.TLocationOptions)
}
const stopForegroundLocation = () => {
tLocation.stopForegroundLocation({
success: (result: tLocation.TLocationResult) => {
console.log(result)
},
fail: (result: tLocation.TLocationResult) => {
console.log(result)
}
} as tLocation.TLocationOptions)
}
const setKey = () => {
tLocation.setKey({
appKey: "你的KEY",
success: (result: tLocation.TLocationResult) => {
console.log(result)
},
fail: (result: tLocation.TLocationResult) => {
console.log(result)
}
} as tLocation.TLocationOptions)
}
const setUserAgreePrivacy = () => {
tLocation.setUserAgreePrivacy()
}
</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 = {
appKey?: string;
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;
}