44 lines
981 B
TypeScript
Raw Normal View History

2025-10-31 11:29:01 +08:00
// 高德地图key
export const GAODE_MAP_KEY = import.meta.env.VITE_AMAP_KEY
// 获取地图实例
export function getMapContext(mapid: string, instance: any) {
return uni.createMapContext(mapid, {
this: instance.proxy,
})
}
// 获取当前定位
export function getLoalcation({
mapContext,
success = null,
fail = null,
moveCenter = false,
}) {
uni.getLocation({
type: 'gcj02',
geocode: true,
success(res) {
console.log('定位成功', res)
const { latitude, longitude } = res
if (moveCenter && mapContext) {
moveToLocation(mapContext, longitude, latitude)
}
success && success({ latitude, longitude })
},
fail(err) {
console.error('uni.getLocation-------', err)
fail && fail(err)
},
})
}
// 移至中心点
export function moveToLocation(mapContext: any, longitude: number, latitude: number) {
const point = {
latitude,
longitude,
}
mapContext.moveToLocation(point)
}