43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
const TENCENT_MAP_KEY = import.meta.env.VITE_TENCENT_MAP_KEY as string | undefined
|
||
|
||
interface TencentGeocodeResponse {
|
||
status?: number
|
||
message?: string
|
||
result?: {
|
||
address?: string
|
||
formatted_addresses?: {
|
||
recommend?: string
|
||
}
|
||
}
|
||
}
|
||
|
||
/** 逆地理编码(需配置 VITE_TENCENT_MAP_KEY) */
|
||
export async function reverseGeocode(
|
||
latitude: number,
|
||
longitude: number,
|
||
): Promise<string | undefined> {
|
||
if (!TENCENT_MAP_KEY) return undefined
|
||
|
||
try {
|
||
const res = await uni.request({
|
||
url: 'https://apis.map.qq.com/ws/geocoder/v1/',
|
||
method: 'GET',
|
||
data: {
|
||
location: `${latitude},${longitude}`,
|
||
key: TENCENT_MAP_KEY,
|
||
get_poi: 0,
|
||
},
|
||
})
|
||
const data = res.data as TencentGeocodeResponse
|
||
if (data.status !== 0) return undefined
|
||
return data.result?.formatted_addresses?.recommend
|
||
|| data.result?.address
|
||
} catch {
|
||
return undefined
|
||
}
|
||
}
|
||
|
||
export function formatCoordinateAddress(latitude: number, longitude: number): string {
|
||
return `纬度 ${latitude.toFixed(4)},经度 ${longitude.toFixed(4)}`
|
||
}
|