更新记录

1.0.3(2025-07-18)

  • 【优化】整体代码
  • 【修复】iOS回调问题

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.xmlInfo.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;
}

隐私、权限声明

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

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/> <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

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

插件使用的 腾讯定位 SDK会采集数据,详情可参考:https://privacy.qq.com/document/preview/dbd484ce652c486cb6d7e43ef12cefb0

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

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