更新记录
1.0.1(2024-08-21)
- 【更新】更新使用文档
- 【新增】新增参数配置
- 【优化】优化底层逻辑
1.0.0(2024-08-17)
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 4.24,Android:5.0,iOS:不确定,HarmonyNext:不确定 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
x-gaode-s【高德定位】
使用教程
在你的项目根目录创建AndroidManifest.xml
文件,引入一下代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="你的包名">
<application>
<meta-data android:name="com.amap.api.v2.apikey" android:value="你的KEY"></meta-data>
</application>
</manifest>
引入
import {
xUpdatePrivacyAgree,
xUpdatePrivacyShow,
xApplyPermissions,
xGetLocation,
xGetOnceLocation,
xStopLocation
} from "@/uni_modules/x-gaode-s"
import {X_RESULT_TYPE,X_LOCATION_TYPE} from "@/uni_modules/x-gaode-s/utssdk/interface"
template模版
<template>
<view>
<view class="item">
<button class="item-btn" style="margin-top: 10;" @click="requestPermission">申请权限</button>
<button class="item-btn" style="margin-top: 10;" @click="getLocation">开启持续定位</button>
<button class="item-btn" style="margin-top: 10;" @click="stopLocation">关闭持续定位</button>
<button class="item-btn" style="margin-top: 10;" @click="getOnceLocation">单次定位</button>
</view>
<view class="content">
<text>longitude:{{longitude}}</text>
<text>latitude:{{latitude}}</text>
</view>
</view>
</template>
Vue2使用示例
<script>
import {
xUpdatePrivacyAgree,
xUpdatePrivacyShow,
xApplyPermissions,
xGetLocation,
xGetOnceLocation,
xStopLocation
} from "@/uni_modules/x-gaode-s"
import {X_RESULT_TYPE,X_LOCATION_TYPE} from "@/uni_modules/x-gaode-s/utssdk/interface"
export default {
data() {
return {
longitude: 0,
latitude: 0
}
},
onReady() {
xUpdatePrivacyShow()
xUpdatePrivacyAgree()
},
methods: {
requestPermission() {
xApplyPermissions((data:X_RESULT_TYPE) => {
console.log(data)
})
},
getOnceLocation() {
uni.showLoading({title:"获取中"})
xGetOnceLocation((result: X_RESULT_TYPE) => {
console.log(result)
this.latitude = (result.data as UTSJSONObject).getNumber("latitude") as number
this.longitude = (result.data as UTSJSONObject).getNumber("longitude") as number
uni.hideLoading()
})
},
getLocation() {
xGetLocation({mode: "Hight_Accuracy"} as X_LOCATION_TYPE,(result: X_RESULT_TYPE) => {
console.log(result)
this.latitude = (result.data as UTSJSONObject).getNumber("latitude") as number
this.longitude = (result.data as UTSJSONObject).getNumber("longitude") as number
})
},
stopLocation() {
uni.showLoading({title:"获取中"})
xStopLocation((result: X_RESULT_TYPE) => {
console.log(result)
uni.hideLoading()
})
}
}
}
</script>
Vue3使用示例
<script lang="uts" setup>
import {
xUpdatePrivacyAgree,
xUpdatePrivacyShow,
xApplyPermissions,
xGetLocation,
xGetOnceLocation,
xStopLocation
} from "@/uni_modules/x-gaode-s"
import {X_RESULT_TYPE,X_LOCATION_TYPE} from "@/uni_modules/x-gaode-s/utssdk/interface"
let longitude = ref(0)
let latitude = ref(0)
const requestPermission = ()=> {
xApplyPermissions((data:X_RESULT_TYPE) => {
console.log(data)
})
}
const getOnceLocation = ()=> {
uni.showLoading({title:"获取中"})
xGetOnceLocation((result: X_RESULT_TYPE) => {
console.log(result)
latitude.value = (result.data as UTSJSONObject).getNumber("latitude") as number
longitude.value = (result.data as UTSJSONObject).getNumber("longitude") as number
uni.hideLoading()
})
}
const getLocation = () => {
xGetLocation({mode: "Hight_Accuracy"} as X_LOCATION_TYPE,(result: X_RESULT_TYPE) => {
console.log(result)
latitude.value = (result.data as UTSJSONObject).getNumber("latitude") as number
longitude.value = (result.data as UTSJSONObject).getNumber("longitude") as number
})
}
const stopLocation = () => {
uni.showLoading({title:"获取中"})
xStopLocation((result: X_RESULT_TYPE) => {
console.log(result)
uni.hideLoading()
})
}
onReady(() => {
xUpdatePrivacyShow()
xUpdatePrivacyAgree()
})
</script>
方法
名称 |
参数 |
描述 |
xApplyPermissions |
- |
请求定位权限 |
xUpdatePrivacyShow |
- |
设置包含隐私政策,并展示用户授权弹窗 |
xUpdatePrivacyAgree |
- |
设置是否同意用户授权政策 |
xGetLocation |
(params:X_LOCATION_TYPE,callback:(data:X_RESULT_TYPE)=>void) |
开启持续定位 |
xStopLocation |
- |
关闭持续定位 |
xGetOnceLocation |
(callback:(data:X_RESULT_TYPE)=>void) |
开启单次定位 |
X_LOCATION_TYPE 传入参数
名称 |
类型 |
必填 |
默认值 |
可选值 |
描述 |
purpose |
String |
否 |
- |
SignIn 或 Sport 或 Transport |
设置定位场景 |
mode |
String |
否 |
Battery_Saving |
Device_Sensors或Hight_Accuracy |
是否允许使用GPS |
interval |
Number |
否 |
2000 |
>1000 |
设置定位间隔时间,单位ms |
needAddress |
Boolean |
否 |
true |
false |
设置是否返回地址信息 |
httpTimeout |
Number |
否 |
30000 |
>8000 |
设置定位请求超时时间,默认30秒,单位ms |
cacheEnable |
Boolean |
否 |
false |
true |
设置是否开启定位缓存机制 |
X_RESULT_TYPE 返回参数
名称 |
类型 |
描述 |
code |
Int |
状态码 |
msg |
string |
状态消息 |
data |
UTSJSONObject |
定位数据 |
X_RESULT_TYPE 的 code和msg
- 详细请查看:https://lbs.amap.com/api/android-location-sdk/guide/utilities/errorcode
code |
msg |
-1 |
相关错误信息请查看控制台 |
0 |
注册位置监听器成功(代表正常) |
X插件系列
x-base-s【基础插件】安卓好用的基础工具
x-sms【短信插件】为X系列插件打造完美轮子
x-tencent-u【腾讯地图】
x-tencent-s【腾讯定位】保活和持久,保姆和小白式的完美解决方案
x-gaode-s【高德定位】保姆和小白式完美解决方案
x-icon-u【图标组件】
x-short-video-u是一个高性能,高度配置的短视频组件
x-im-sdk 为即时通讯量身打造, 适配Android,iOS,Web