Files
czzj-app/src/utils/map-geocode.ts
2026-10-13 16:23:43 +08:00

43 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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)}`
}