鸿蒙定位SDK
概述
维智定位SDK(HarmonyOS NEXT)
是维智开放平台面向 HarmonyOS NEXT 端移动应用开发者全新推出的一款定位服务产品。开发者在自己的 HarmonyOS NEXT 移动应用中集成定位 SDK,可轻松实现获取当前位置信息的功能。
以 HarmonyOS NEXT 系统定位能力为基础实现基础定位、后台定位能力,在此基础之上提供丰富的地址能力,包括:获取定位地址、位置描述等功能;
赋能开发者 - 提供定位鸿蒙原生 ArkTS 开发接口
- 采用鸿蒙 NEXT 推荐的 ArkTS 语言与 ArkUI 原生组件
- 全量适配鸿蒙 NEXT SDK 与系统 API
- 兼容方舟 UI 框架,支持声明式开发范式
接口易用性 - 保持跨平台一致性设计
- 与 Android / 鸿蒙历史版本保持一致
- 100% 继承原有架构设计,支持无缝迁移
- 提供自动化适配工具,降低 90% 迁移成本
开发指南
获取KEY
- 创建应用
进入控制台,创建一个新应用。如果您之前已经创建过应用,可直接跳过这个步骤。


审核通过后,即可获取密钥 accessKey。
项目创建
工程配置
1. 打开/创建一个鸿蒙工程
使用 DevEco Studio 打开一个鸿蒙工程,或者新建一个鸿蒙工程
2. SDK 导入
(1)本地引用
将从官网下载的 har文件放到您工程中 entry 的libs目录中,如下图所示

在entry目录下的oh-package.json5中引用,如下图所示

"@ohos/wzLocation": "file:libs/wzLocation.har"
3. 权限配置
在module.json5文件中配置 HarmonyOS NEXT 定位 SDK 所需的相关权限,确保 SDK 可以正常使用。配置如下:
"requestPermissions": [
{
"name": "ohos.permission.LOCATION",
"reason": "允许应用在前台运行时获取位置信息",
},
{
"name": "ohos.permission.LOCATION_IN_BACKGROUND",
"reason": "允许应用在后台运行时获取位置信息",
},
{
"name": "ohos.permission.APPROXIMATELY_LOCATION",
"reason": "允许应用获取设备模糊位置信息",
},
{
"name": "ohos.permission.APP_TRACKING_CONSENT",
"reason": "允许应用获取设备唯一标识符",
},
{
"name": "ohos.permission.GET_WIFI_INFO",
"reason": "允许应用获取连接wifi信息",
},
{
"name": "ohos.permission.GET_NETWORK_INFO",
"reason": "允许应用获取网络信息",
},
{
"name": "ohos.permission.INTERNET",
"reason": "允许应用访问网络",
},
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING",
"reason": "允许应用进行长时任务",
}
]
4. AK 配置
this.wzLocation.initOption(官网申请accessKey, 设备ID, 定位频率);
获取位置信息
单次定位
维智 HarmonyOS NEXT 定位 sdk 提供单次定位的能力。具体使用方法如下:
wzLocation: WzLocation = new WzLocation();
// 定位接口回调
setLocationCallback() {
this.wzLocation.setLocationCall({
success: (location?: LocationRes) => {
if (location) {
console.log('定位成功:', location);
this.locationResult = `定位成功: 纬度 ${location.location.position.point.latitude}, 经度 ${location.location.position.point.longitude}, 地址 ${location.location.address.name}`;
} else {
console.log('定位成功,但返回的位置信息为空');
this.locationResult = '定位成功,但返回的位置信息为空';
}
},
fail: (error: ErrorInfo) => {
console.error('定位失败:' + error.msg);
this.locationResult = `定位失败: ${error.msg}`;
}
});
}
// 初始化定位配置
initLocationOption(interval: number) {
this.wzLocation.initOption(申请的accessKey, 设备ID, 持续定位时间--单位秒);
}
// 启动单次定位
this.initLocationOption(0);
this.setLocationCallback();
console.error('测试单次定位:-----》');
this.wzLocation.startLocation().then(() => {
console.log('单次定位已启动');
});
后台定位(持续定位)
wzLocation: WzLocation = new WzLocation();
// 定位接口回调
setLocationCallback() {
this.wzLocation.setLocationCall({
success: (location?: LocationRes) => {
if (location) {
console.log('定位成功:', location);
this.locationResult = `定位成功: 纬度 ${location.location.position.point.latitude}, 经度 ${location.location.position.point.longitude}, 地址 ${location.location.address.name}`;
} else {
console.log('定位成功,但返回的位置信息为空');
this.locationResult = '定位成功,但返回的位置信息为空';
}
},
fail: (error: ErrorInfo) => {
console.error('定位失败:' + error.msg);
this.locationResult = `定位失败: ${error.msg}`;
}
});
}
// 初始化定位配置
initLocationOption(interval: number) {
this.wzLocation.initOption(申请的accessKey, 设备ID, 持续定位时间--单位秒);
}
// 启动持续定位
this.initLocationOption(5);
this.setLocationCallback();
console.error('测试连续定位-----》:');
this.wzLocation.startContinuousLocation();
获取地址
wzLocation: WzLocation = new WzLocation();
// 定位接口回调
setLocationCallback() {
this.wzLocation.setLocationCall({
success: (location?: LocationRes) => {
if (location) {
console.log('定位成功:', location);
this.locationResult = `定位成功地址: ${location.location.address.name}`;
} else {
console.log('定位成功,但返回的位置信息为空');
this.locationResult = '定位成功,但返回的位置信息为空';
}
},
fail: (error: ErrorInfo) => {
console.error('定位失败:' + error.msg);
this.locationResult = `定位失败: ${error.msg}`;
}
});
}
// 获取地址信息接口
this.initLocationOption(0);
this.setLocationCallback();
this.getAddressFromWZ({latitude: 23.72398754219176,longitude: 113.10165074642399})
隐私合规
1. 定位的合规接口说明如下:
/**
* 设置是否同意用户授权政策 必须在定位实例化之前调用
*
* @param isAgree 隐私权政策是否取得用户同意 true 是用户同意
*/
setAgreePrivacy(isAgree: boolean)
权限说明
权限配置:在 module.json5 文件中配置 HarmonyOS NEXT 定位 SDK 所需的相关权限,确保 SDK 可以正常使用。配置如下:
"requestPermissions": [
{
"name": "ohos.permission.LOCATION",
"reason": "允许应用在前台运行时获取位置信息",
},
{
"name": "ohos.permission.LOCATION_IN_BACKGROUND",
"reason": "允许应用在后台运行时获取位置信息",
},
{
"name": "ohos.permission.APPROXIMATELY_LOCATION",
"reason": "允许应用获取设备模糊位置信息",
},
{
"name": "ohos.permission.APP_TRACKING_CONSENT",
"reason": "允许应用获取设备唯一标识符",
},
{
"name": "ohos.permission.GET_WIFI_INFO",
"reason": "允许应用获取连接wifi信息",
},
{
"name": "ohos.permission.GET_NETWORK_INFO",
"reason": "允许应用获取网络信息",
},
{
"name": "ohos.permission.INTERNET",
"reason": "允许应用访问网络",
},
{
"name": "ohos.permission.KEEP_BACKGROUND_RUNNING",
"reason": "允许应用进行长时任务",
}
]
权限申请:在应用入口开始主动申请定位权限
let atManager = abilityAccessCtrl.createAtManager();
try {
atManager.requestPermissionsFromUser(this.context,
['ohos.permission.LOCATION', 'ohos.permission.APPROXIMATELY_LOCATION'])
.then((data) => {
hilog.info(0x0000, TAG, `data: ${JSON.stringify(data)}`);
})
.catch((err: BusinessError) => {
hilog.error(0x0000, TAG, `err: ${JSON.stringify(err)}`);
})
} catch (err) {
hilog.error(0x0000, TAG, `catch err->${JSON.stringify(err)}`);
}
功能:
依托HarmonyOS NEXT原生定位能力,提供单次定位、持续定位、后台定位等基础定位能力,并扩展逆地理编码、正地理编码、POI 搜索、坐标系转换等丰富的地址与位置服务能力。
SDK基础信息


更多推荐

所有评论(0)