更新记录
1.0.2(2025-11-01)
- 适配鸿蒙端
1.0.1(2025-08-12)
- 优化Android权限
- 优化保活定位
1.0.0(2025-04-21)
新版发布
查看更多平台兼容性
uni-app(4.66)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | × | × | √ | √ | 5.0 | 12 | 12 |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|
| × | × | × | × | × | × | × | × | × | × | × |
uni-app x(4.66)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| × | × | 5.0 | 12 | 12 | × |
特别提醒
- 购买本插件前,请先试用、请先试用、请先试用,并充分自测确认满足需求之后再行购买。虚拟物品一旦购买之后无法退款;
- 如有使用上的疑问、bug,可以进交流群联系作者;
- 作者可承接各种插件定制;
- 请在合法范围内使用,若使用本插件做非法开发,本方概不负责;
- 结合文档和示例代码集成使用;
- uniapp使用本插件保活、后台定位功能iOS需在manifest.json/其他设置中后台运行能力配置location
- android uniapp使用需设置targetSdkVersion >=29(在manifest.json->安卓/iOS常用其他设置)
- iOS/Android需更换appKey,更换位置见下面集成步骤
- 试用必须先打自定义基座;
插件功能介绍 (支持uni-app/uni-app-x)
- 定位、连续定位、后台定位(Android需要使用保活定位)
- 坐标转换
- 保活
Android后台采集或保活设置
-
通知管理允许通知,允许自启动;
-
耗电保护允许后台运行;
-
电池设置中关闭智能耗电保护、省电模式、应用速冻等。
注意事项:
- 本插件使用需要打自定义基座调试、试用。
- 打基座、打包前要先配置百度定位key iOS:uni_modules->yt-uts-bdlocation->utssdk->app-ios->info.plist文件中 Android:uni_modules->yt-uts-bdlocation->utssdk->app-android->AndroidManifest.xml文件中;
- Android手机型号太多,各品牌对手机的限制不尽相同,需要保活而进行的设置也不一样。随着Android系统的更新迭代,保活的难度也在不断加大。最有效的保活方式还是联系厂家添加白名单以实现进程保活。
- iOS保活定位调用连续定位设置isAllow=true,andoid保活定位调用keepAlive方法,更多参数说明可进入uni_modules->yt-uts-bdlocation->interface.uts文件查看,里面有各个参数的详细说明。
特别注意HarmonyOS后台定位需配置后台功能。配置步骤如下:
- 点击运行->运行到手机/模拟器->运行到鸿蒙
- 运行成功会在项目根目录下生成unpackage文件夹,找到unpackage/dist/dev/app-harmony/entry,将该文件夹复制到项目根目录下的harmony-configs文件夹下
- 在harmony-configs/entry/src/main/module.json5文件中增加设置如一下代码
在harmony-configs/entry/src/main/module.json5文件中增加设置
{
"module": {
"abilities": [
{
...
"backgroundModes": [
"location"
],
"continuable": true
}
}
}
}
定位
定位需要注意的地方:
- iOS后台定位定位间隔不能设置太大,应小于12秒
- 3.Android后台定位,请使用保活定位。
api说明
初始化定位插件:建议放在onLoad中
// #ifdef APP-HARMONY
//用于获取appid用于申请百度定位、地图的appkey
let appId = BDLocation.getHarmonyOSAppID()
console.log(`appId:${appId}`)
//参数高德开放平台申请的Appkey 换成自己的(百度地图开放平台申请)
BDLocation.initLocal("f7SGmokCggeOrUCNJi224nmzbScuWFNz")
// #endif
// #ifdef APP-ANDROID
//Android和iOS传空,Android和iOS在其他地方设置 Android:uni_modules->yt-uts-bdlocation->utssdk->app-android->AndroidManifest.xml文件中 iOS:uni_modules->yt-uts-bdlocation->utssdk->app-ios->info.plist文件中
BDLocation.initLocal("");
// #endif
// #ifdef APP-IOS
//Android和iOS传空,Android和iOS在其他地方设置 Android:uni_modules->yt-uts-bdlocation->utssdk->app-android->AndroidManifest.xml文件中 iOS:uni_modules->yt-uts-bdlocation->utssdk->app-ios->info.plist文件中
BDLocation.initLocal("");
// #endif
//设置Androidnotification标题与内容
BDLocation.setNotificationTitle("***", "***");
单次定位:uniapp项目去掉 as BDLocation.LocationOptions 类型转换不然会报错,详情见示例代码。单次定位入参说明详情见:uni_modules/yt-uts-bdlocation/utssdk/interface.uts文件
BDLocation.onceLocation({
locationMode: 'Battery_Saving',
locationTimeout: 10000,
coorType: 'bd09ll',
reGeocode: true,
openGps: true,
resultBackcall: (res) => {
if (res.code == 200) {
let lat = res.lat ?? 0.0;
let lng = res.lng ?? 0.0;
this.lat = lat;
this.lng = lng;
console.log(`精度:${lng} 纬度:${lat}`)
uni.showToast({
title: `定位成功精度:${lng}---纬度:${lat}`,
icon: 'none'
})
} else {
console.log(`定位失败:${res.loc_type_description ?? ""}--LocType=${res.locType ?? 0} 前往百度定位官网对照错误码:https://lbsyun.baidu.com/faq/api?title=android-locsdk/guide/addition-func/error-code`)
uni.showToast({
title: res.loc_type_description ?? "",
icon: 'none'
})
}
}
} as BDLocation.LocationOptions)
连续定位:uniapp项目去掉 as BDLocation.LocationOptions 类型转换不然会报错,详情见示例代码。连续定位入参说明详情见:uni_modules/yt-uts-bdlocation/utssdk/interface.uts文件
BDLocation.startUpdatingLocation({
locationMode: 'Battery_Saving',
locationTimeout: 10000,
coorType: 'gcj02',
reGeocode: true,
openGps: true,
scanSpan: 60000,
isAllow: true,
resultBackcall: (res) => {
this.locationCount++;
console.log(res)
if (res.code == 200) {
// console.log(res.address ?? "");
let lat = res.lat ?? 0.0;
let lng = res.lng ?? 0.0;
uni.showToast({
title: `定位成功次数:${this.locationCount}精度:${lng}-纬度:${lat}`,
icon: 'none'
})
};
}
} as BDLocation.LocationOptions);
///停止连续定位
BDLocation.stopUpdatingLocation()
保活定位:uniapp项目去掉 as BDLocation.KeepLiveOptions 类型转换不然会报错,详情见示例代码。连续定位入参说明详情见:uni_modules/yt-uts-bdlocation/utssdk/interface.uts文件
BDLocation.keepAlive({
openGps: true,
scanSpan: 40000,
resultBackcall: (res) => {
console.log(res);
this.locationCount++;
uni.showToast({
icon: 'none',
title: `第${this.locationCount}次定位精度:${res.lng}---纬度:${res.lat}`
})
}
} as BDLocation.KeepLiveOptions)
//停止保活定位
BDLocation.nonKeepAlive()
uniapp 示例代码
<template>
<view class="content">
<view class="header">
<view class="btn" @click="onceLocationAction()">单次定位</view>
<view class="btn" @click="startUpdatingLocationAction()">连续定位</view>
<view class="btn" @click="keepAliveAction()">保活定位</view>
</view>
<view class="header">
<view class="btn" @click="stopUpdatingLocationAction()">停止连续定位</view>
<view class="btn" @click="nonKeepAliveAction()">停止保活</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="gcj02ToBd09()">
gcj02ToBd09
</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="bd09ToGcj02()">
bd09ToGcj02
</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="bd09ToWgs84()">
bd09ToWgs84
</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="gcj02ToWgs84()">
gcj02ToWgs84
</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="wgs84ToBd09()">
wgs84ToBd09
</view>
</view>
</view>
</template>
<script>
import * as BDLocation from "@/uni_modules/yt-uts-bdlocation"
export default {
data() {
return {
locationCount: 0,
lat: 0.00,
lng: 0.00
}
},
onLoad() {
// #ifdef APP-HARMONY
//用于获取appid用于申请百度定位、地图的appkey
let appId = BDLocation.getHarmonyOSAppID()
console.log(`appId:${appId}`)
//参数高德开放平台申请的Appkey 换成自己的(百度地图开放平台申请)
BDLocation.initLocal("f7SGmokCggeOrUCNJi224nmzbScuWFNz")
// #endif
// #ifdef APP-ANDROID
//Android和iOS传空,Android和iOS在其他地方设置 Android:uni_modules->yt-uts-bdlocation->utssdk->app-android->AndroidManifest.xml文件中 iOS:uni_modules->yt-uts-bdlocation->utssdk->app-ios->info.plist文件中
BDLocation.initLocal("");
// #endif
// #ifdef APP-IOS
//Android和iOS传空,Android和iOS在其他地方设置 Android:uni_modules->yt-uts-bdlocation->utssdk->app-android->AndroidManifest.xml文件中 iOS:uni_modules->yt-uts-bdlocation->utssdk->app-ios->info.plist文件中
BDLocation.initLocal("");
// #endif
//设置Androidnotification标题与内容
BDLocation.setNotificationTitle("***", "***");
},
methods: {
onceLocationAction() {
BDLocation.onceLocation({
locationMode: 'Battery_Saving',
locationTimeout: 10000,
coorType: 'bd09ll',
reGeocode: true,
openGps: true,
resultBackcall: (res) => {
if (res.code == 200) {
let lat = res.lat ?? 0.0;
let lng = res.lng ?? 0.0;
this.lat = lat;
this.lng = lng;
console.log(`精度:${lng} 纬度:${lat}`)
uni.showToast({
title: `定位成功精度:${lng}---纬度:${lat}`,
icon: 'none'
})
} else {
console.log(`定位失败:${res.loc_type_description ?? ""}--LocType=${res.locType ?? 0} 前往百度定位官网对照错误码:https://lbsyun.baidu.com/faq/api?title=android-locsdk/guide/addition-func/error-code`)
uni.showToast({
title: res.loc_type_description ?? "",
icon: 'none'
})
}
}
});
},
//连续定位
startUpdatingLocationAction() {
BDLocation.startUpdatingLocation({
locationMode: 'Battery_Saving',
locationTimeout: 10000,
coorType: 'gcj02',
reGeocode: true,
openGps: true,
scanSpan: 60000,
isAllow: true,
resultBackcall: (res) => {
this.locationCount++;
console.log(res)
if (res.code == 200) {
// console.log(res.address ?? "");
let lat = res.lat ?? 0.0;
let lng = res.lng ?? 0.0;
uni.showToast({
title: `定位成功次数:${this.locationCount}精度:${lng}-纬度:${lat}`,
icon: 'none'
})
};
}
});
},
//停止连续定位
stopUpdatingLocationAction() {
BDLocation.stopUpdatingLocation()
},
//保活
keepAliveAction() {
BDLocation.keepAlive({
openGps: true,
scanSpan: 40000,
resultBackcall: (res) => {
console.log(res);
this.locationCount++;
uni.showToast({
icon: 'none',
title: `第${this.locationCount}次定位精度:${res.lng}---纬度:${res.lat}`
})
}
})
},
//停止保活
nonKeepAliveAction() {
BDLocation.nonKeepAlive()
},
//gcj02ToBd09
gcj02ToBd09() {
if (this.lat == 0) return;
console.log(BDLocation.gcj02ToBd09(this.lat, this.lng))
},
//bd09ToGcj02
bd09ToGcj02() {
if (this.lat == 0) return;
console.log(BDLocation.bd09ToGcj02(this.lat, this.lng))
},
//bd09ToWgs84
bd09ToWgs84() {
if (this.lat == 0) return;
console.log(BDLocation.bd09ToWgs84(this.lat, this.lng))
},
//gcj02ToWgs84
gcj02ToWgs84() {
if (this.lat == 0) return;
console.log(BDLocation.gcj02ToWgs84(this.lat, this.lng))
},
//wgs84ToBd09
wgs84ToBd09() {
if (this.lat == 0) return;
console.log(BDLocation.wgs84ToBd09(this.lat, this.lng))
}
}
}
</script>
<style>
.content {
width: 750rpx;
display: flex;
height: 100%;
flex-direction: column;
}
.header {
width: 750rpx;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 0 10rpx;
margin-top: 40rpx;
box-sizing: border-box;
}
.btn {
display: flex;
height: 80rpx;
flex: 1;
align-items: center;
justify-content: center;
background-color: cadetblue;
color: white;
margin-right: 10rpx;
font-size: 28rpx;
}
</style>
uni-app-x示例代码
<template>
<view class="content">
<view class="header">
<view class="btn" @click="onceLocationAction()">单次定位</view>
<view class="btn" @click="startUpdatingLocationAction()">连续定位</view>
<view class="btn" @click="keepAliveAction()">保活定位</view>
</view>
<view class="header">
<view class="btn" @click="stopUpdatingLocationAction()">停止连续定位</view>
<view class="btn" @click="nonKeepAliveAction()">停止保活</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="gcj02ToBd09()">
gcj02ToBd09
</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="bd09ToGcj02()">
bd09ToGcj02
</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="bd09ToWgs84()">
bd09ToWgs84
</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="gcj02ToWgs84()">
gcj02ToWgs84
</view>
</view>
<view class="header" style="margin-top: 30rpx;">
<view class="btn" @click="wgs84ToBd09()">
wgs84ToBd09
</view>
</view>
</view>
</template>
<script>
import * as BDLocation from "@/uni_modules/yt-uts-bdlocation"
export default {
data() {
return {
locationCount: 0,
lat: 0.00,
lng: 0.00
}
},
onLoad() {
// #ifdef APP-HARMONY
//用于获取appid用于申请百度定位、地图的appkey
let appId = BDLocation.getHarmonyOSAppID()
console.log(`appId:${appId}`)
//参数高德开放平台申请的Appkey 换成自己的(百度地图开放平台申请)
BDLocation.initLocal("f7SGmokCggeOrUCNJi224nmzbScuWFNz")
// #endif
// #ifdef APP-ANDROID
//Android和iOS传空,Android和iOS在其他地方设置 Android:uni_modules->yt-uts-bdlocation->utssdk->app-android->AndroidManifest.xml文件中 iOS:uni_modules->yt-uts-bdlocation->utssdk->app-ios->info.plist文件中
BDLocation.initLocal("");
// #endif
// #ifdef APP-IOS
//Android和iOS传空,Android和iOS在其他地方设置 Android:uni_modules->yt-uts-bdlocation->utssdk->app-android->AndroidManifest.xml文件中 iOS:uni_modules->yt-uts-bdlocation->utssdk->app-ios->info.plist文件中
BDLocation.initLocal("");
// #endif
//设置Androidnotification标题与内容
BDLocation.setNotificationTitle("***", "***");
},
methods: {
onceLocationAction() {
BDLocation.onceLocation({
locationMode: 'Battery_Saving',
locationTimeout: 10000,
coorType: 'bd09ll',
reGeocode: true,
openGps: true,
resultBackcall: (res) => {
if (res.code == 200) {
let lat = res.lat ?? 0.0;
let lng = res.lng ?? 0.0;
this.lat = lat;
this.lng = lng;
console.log(`精度:${lng} 纬度:${lat}`)
uni.showToast({
title: `定位成功精度:${lng}---纬度:${lat}`,
icon: 'none'
})
} else {
console.log(`定位失败:${res.loc_type_description ?? ""}--LocType=${res.locType ?? 0} 前往百度定位官网对照错误码:https://lbsyun.baidu.com/faq/api?title=android-locsdk/guide/addition-func/error-code`)
uni.showToast({
title: res.loc_type_description ?? "",
icon: 'none'
})
}
}
} as BDLocation.LocationOptions);
},
//连续定位
startUpdatingLocationAction() {
BDLocation.startUpdatingLocation({
locationMode: 'Battery_Saving',
locationTimeout: 10000,
coorType: 'gcj02',
reGeocode: true,
openGps: true,
scanSpan: 60000,
isAllow: true,
resultBackcall: (res) => {
this.locationCount++;
console.log(res)
if (res.code == 200) {
// console.log(res.address ?? "");
let lat = res.lat ?? 0.0;
let lng = res.lng ?? 0.0;
uni.showToast({
title: `定位成功次数:${this.locationCount}精度:${lng}-纬度:${lat}`,
icon: 'none'
})
};
}
} as BDLocation.LocationOptions);
},
//停止连续定位
stopUpdatingLocationAction() {
BDLocation.stopUpdatingLocation()
},
//保活
keepAliveAction() {
BDLocation.keepAlive({
openGps: true,
scanSpan: 40000,
resultBackcall: (res) => {
console.log(res);
this.locationCount++;
uni.showToast({
icon: 'none',
title: `第${this.locationCount}次定位精度:${res.lng}---纬度:${res.lat}`
})
}
} as BDLocation.KeepLiveOptions)
},
//停止保活
nonKeepAliveAction() {
BDLocation.nonKeepAlive()
},
//gcj02ToBd09
gcj02ToBd09() {
if (this.lat == 0) return;
console.log(BDLocation.gcj02ToBd09(this.lat, this.lng))
},
//bd09ToGcj02
bd09ToGcj02() {
if (this.lat == 0) return;
console.log(BDLocation.bd09ToGcj02(this.lat, this.lng))
},
//bd09ToWgs84
bd09ToWgs84() {
if (this.lat == 0) return;
console.log(BDLocation.bd09ToWgs84(this.lat, this.lng))
},
//gcj02ToWgs84
gcj02ToWgs84() {
if (this.lat == 0) return;
console.log(BDLocation.gcj02ToWgs84(this.lat, this.lng))
},
//wgs84ToBd09
wgs84ToBd09() {
if (this.lat == 0) return;
console.log(BDLocation.wgs84ToBd09(this.lat, this.lng))
}
}
}
</script>
<style>
.content {
width: 750rpx;
display: flex;
height: 100%;
flex-direction: column;
}
.header {
width: 750rpx;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 0 10rpx;
margin-top: 40rpx;
box-sizing: border-box;
}
.btn {
display: flex;
height: 80rpx;
flex: 1;
align-items: center;
justify-content: center;
background-color: cadetblue;
color: white;
margin-right: 10rpx;
font-size: 28rpx;
}
</style>

收藏人数:
购买源码授权版(
试用
赞赏(0)
下载 225
赞赏 0
下载 12176799
赞赏 1828
赞赏
京公网安备:11010802035340号