liujie
2026-08-27 c919774537eceacb457b081782029efed93eb28c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function normalizeAddress(location = {}) {
  const address = location.address || ''
  if (address) {
    return address
  }
  const parts = [
    location.province,
    location.city,
    location.district,
    location.street,
    location.streetNum,
    location.poiName
  ].filter(Boolean)
  return parts.join(' ')
}
 
function normalizeLocation(location = {}) {
  return {
    latitude: Number(location.latitude || 0),
    longitude: Number(location.longitude || 0),
    accuracy: Number(location.accuracy || 0),
    address: normalizeAddress(location),
    province: location.province || '',
    city: location.city || '',
    district: location.district || '',
    street: location.street || '',
    streetNum: location.streetNum || '',
    poiName: location.poiName || ''
  }
}
 
export function formatLocationMessage(location = {}) {
  const normalized = normalizeLocation(location)
  const address = normalized.address || '未解析到地址'
  return [
    `纬度:${normalized.latitude.toFixed(6)}`,
    `经度:${normalized.longitude.toFixed(6)}`,
    `精度:${normalized.accuracy || 0}米`,
    `地址:${address}`
  ].join('\n')
}
 
export function getCurrentLocation() {
  return new Promise((resolve, reject) => {
    uni.getLocation({
      type: 'gcj02',
      geocode: true,
      isHighAccuracy: true,
      highAccuracyExpireTime: 4000,
      success(res) {
        resolve(normalizeLocation(res))
      },
      fail(error) {
        reject(error)
      }
    })
  })
}
 
export function requestStartupLocationPermission() {
  return getCurrentLocation().catch((error) => {
    console.log('Location permission request skipped or denied', error)
    return null
  })
}