Skip to content
广告位招租广告位招租

插件地址:前往地址

兼容端

安卓苹果Web鸿蒙小程序

使用示例

vue

<script setup lang="uts">
	import * as tLocation from "@/uni_modules/t-tlocation-api"
	
	
	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)
			},
			fail: (result: tLocation.TLocationResult) => {
				console.log(result)
			}
		} as tLocation.TLocationOptions)
	}
	
	const stopContinuationLocation = () => {
		tLocation.stopContinuationLocation({
			success: (result: tLocation.TLocationResult) => {
				console.log(result)
			},
			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: "YD6BZ-SOY3W-XLJRN-YQW3P-YNOLZ-AEFCO",
			success: (result: tLocation.TLocationResult) => {
				console.log(result)
			},
			fail: (result: tLocation.TLocationResult) => {
				console.log(result)
			}
		} as tLocation.TLocationOptions)
	}
	const setUserAgreePrivacy = () => {
		tLocation.setUserAgreePrivacy()
	}
	onReady(() => {
		setUserAgreePrivacy()
		setKey()
	})
</script>

暴露的类型

ts
/**
 * @property {Number} latitude 纬度
 * @property {Number} longitude 经度
 * @property {String} address 地址
 * @property {String} country 国家
 * @property {String} province 省份
 * @property {String} city 城市
 * @property {String} district 区县
 * @property {String} street 街道信息
 * @property {String} adcode adcode
 * @property {String} town 乡镇信息
 */
export type TBaiduLocationData = {
	latitude?: number;
	longitude?: number;
	address?: string;
	country?: string;
	province?: string;
	city?: string;
	district?: string;
	street?: string;
	adcode?: string;
	town?: string;
}
/**
 * @description 返回信息
 * @property {Number} code 状态码
 * @property {String} msg 状态消息
 * @property {TBaiduLocationData} data 定位数据
 */
export type TBaiduLocationResult = {
	code: number;
	msg: string;
	data?: TBaiduLocationData;
}
/**
 * @property {Number} mode 定位模式
 * @value 1 高精度
 * @value 2 低功耗
 * @value 3 仅使用设备
 * @value 4 模糊定位模式
 * @property {String} coorType 经纬度坐标类型
 * @value gcj02 国测局坐标
 * @value bd09ll 百度经纬度坐标
 * @value bd09 百度墨卡托坐标
 * @property {Boolean} needAddress 是否需要地址信息
 * @property {Boolean} needNewVersionRgc 设置是否需要最新版本的地址信息
 * @property {Boolean} needLocationDescribe 是否需要位置描述信息
 * @property {Boolean} needLocationPoiList 是否需要周边POI信息
 * @property {Number} scene 场景模式,仅场景定位有效
 * @value 1 签到场景
 * @value 2 运动场景
 * @value 3 出行场景
 */
export type TBaiduLocationOptions = {
	mode?: number;
	coorType?: "gcj02" | "bd09ll" | "bd09";
	needAddress?: boolean;
	needNewVersionRgc?: boolean;
	needLocationDescribe?: boolean;
	needLocationPoiList?: boolean;
	scene?: number;
	success?: (result: TBaiduLocationResult) => void;
	fail?: (result: TBaiduLocationResult) => void;
}