更新记录

1.0.2(2025-03-25)

  • 【更新】修复版本兼容问题,修复老版本bug

1.0.1(2024-08-21)

  • 【更新】更新使用文档
  • 【新增】新增参数配置
  • 【优化】优化底层逻辑

1.0.0(2024-08-17)

  • 初始化第一版本
查看更多

平台兼容性

Vue2 Vue3
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>
  • 注意插件在引入项目的时候,隐私合规政策就已经自动为true

引入

import { 
    gaodeLocation,
    X_RESULT_TYPE,
    X_LOCATION_OPTIONS
} from "@/uni_modules/x-gaode-s"

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 { 
    gaodeLocation,
    X_RESULT_TYPE,
    X_LOCATION_OPTIONS
} from "@/uni_modules/x-gaode-s"
    export default {
        data() {
            return {
                longitude: 0,
                latitude: 0
            }
        },
        methods: {
            requestPermission() {
                gaodeLocation.xApplyPermissions({
                    success: (result: X_RESULT_TYPE) => {
                        console.log(result)
                    },
                })
            },
            getOnceLocation() {
                uni.showLoading({title:"获取中"})
                gaodeLocation.xGetOnceLocation({
                    needAddress: true,
                    success: (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()
                    },
                    fail: (result: X_RESULT_TYPE) => {
                        console.log(result)
                    }
                } as X_LOCATION_OPTIONS)
            },
            getLocation() {
                gaodeLocation.xGetLocation({
                     mode: "Hight_Accuracy",
                     needAddress: true,
                     success: (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
                     },
                     fail: (result: X_RESULT_TYPE) => {
                        console.log(result)
                     }
                } as X_LOCATION_OPTIONS)
            },
            stopLocation() {
                uni.showLoading({title:"获取中"})
                gaodeLocation.xStopLocation({
                    success: () => {
                        uni.hideLoading()
                    },
                    fail: () => {
                        uni.hideLoading()
                    }
                })
            }
        }
    }
</script>

Vue3使用示例

<script lang="uts" setup>
import { 
    gaodeLocation,
    X_RESULT_TYPE,
    X_LOCATION_OPTIONS
} from "@/uni_modules/x-gaode-s"

let longitude = ref(0)
let latitude = ref(0)

const requestPermission = ()=> {
    gaodeLocation.xApplyPermissions({
        success: (result: X_RESULT_TYPE) => {
            console.log(result)
        },
    })
}
const getOnceLocation = ()=> {
    uni.showLoading({title:"获取中"})
    gaodeLocation.xGetOnceLocation({
        needAddress: true,
        success: (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()
        },
        fail: (result: X_RESULT_TYPE) => {
            console.log(result)
        }
    } as X_LOCATION_OPTIONS)
}
const getLocation = () => {
    gaodeLocation.xGetLocation({
         mode: "Hight_Accuracy",
         needAddress: true,
         success: (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
         },
         fail: (result: X_RESULT_TYPE) => {
            console.log(result)
         }
    } as X_LOCATION_OPTIONS)
}
const stopLocation = () => {
    stopLocation() {
        uni.showLoading({title:"获取中"})
        gaodeLocation.xStopLocation({
            success: () => {
                uni.hideLoading()
            },
            fail: () => {
                uni.hideLoading()
            }
        })
    }
}
</script>

方法

名称 参数 描述
gaodeLocation.xApplyPermissions (options:X_LOCATION_OPTIONS) 请求定位权限
gaodeLocation.xGetLocation (options:X_LOCATION_OPTIONS) 开启持续定位
gaodeLocation.xStopLocation (options:X_LOCATION_OPTIONS) 关闭持续定位
gaodeLocation.xGetOnceLocation (options:X_LOCATION_OPTIONS) 开启单次定位

X_LOCATION_OPTIONS 传入参数

名称 类型 必填 默认值 可选值 描述
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 设置是否开启定位缓存机制
success (res:X_RESULT_TYPE) => void; - - 成功回调
fail (res:X_RESULT_TYPE) => void; - - 失败回调

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

隐私、权限声明

1. 本插件需要申请的系统权限列表:

<!--允许访问网络,必选权限--> <uses-permission android:name="android.permission.INTERNET" /> <!--允许获取精确位置,精准定位必选--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!--允许获取粗略位置,若用GPS实现定位小蓝点功能则必选--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!--允许获取网络状态,用于网络定位,若无gps但仍需实现定位小蓝点功能则此权限必选--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--允许获取wifi网络信息,用于网络定位,若无gps但仍需实现定位小蓝点功能则此权限必选--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!--允许获取wifi状态改变,用于网络定位,若无gps但仍需实现定位小蓝点功能则此权限必选--> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!--允许写入扩展存储,用于数据缓存,若无此权限则写到私有目录--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--允许写设备缓存,用于问题排查--> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!--允许读设备等信息,用于问题排查--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!--后台获取位置信息,若需后台定位则必选--> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <!--用于申请调用A-GPS模块,卫星定位加速--> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问