'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
  getJSONP
} from '../../../helpers/get-jsonp'
import {
  MapType,
  getMapInfo,
  translateCoordinateSystem
} from '../../../helpers/location'
import { loadMaps } from '../../../view/components/map/maps'
 
/**
 * 获取定位信息
 * @param {*} options
 * @param {*} callbackId
 */
export function getLocation ({
  type,
  altitude,
  isHighAccuracy,
  highAccuracyExpireTime
}, callbackId) {
  const {
    invokeCallbackHandler: invoke
  } = UniServiceJSBridge
  const mapInfo = getMapInfo()
 
  new Promise((resolve, reject) => {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(res => resolve({ coords: res.coords }), reject, {
        enableHighAccuracy: isHighAccuracy || altitude,
        timeout: highAccuracyExpireTime || 1000 * 100
      })
    } else {
      reject(new Error('device nonsupport geolocation'))
    }
  }).catch(() => {
    return new Promise((resolve, reject) => {
      if (mapInfo.type === MapType.QQ) {
        getJSONP(`https://apis.map.qq.com/ws/location/v1/ip?output=jsonp&key=${mapInfo.key}`, {
          callback: 'callback'
        }, (res) => {
          if ('result' in res && res.result.location) {
            const location = res.result.location
            resolve({
              coords: {
                latitude: location.lat,
                longitude: location.lng
              },
              skip: true
            })
          } else {
            reject(new Error(res.message || JSON.stringify(res)))
          }
        }, () => reject(new Error('network error')))
      } else if (mapInfo.type === MapType.GOOGLE) {
        uni.request({
          method: 'POST',
          url: `https://www.googleapis.com/geolocation/v1/geolocate?key=${mapInfo.key}`,
          success (res) {
            const data = res.data
            if ('location' in data) {
              resolve({
                coords: {
                  latitude: data.location.lat,
                  longitude: data.location.lng,
                  accuracy: data.accuracy
                },
                skip: true
              })
            } else {
              reject(new Error((data.error && data.error.message) || JSON.stringify(res)))
            }
          },
          fail () {
            reject(new Error('network error'))
          }
        })
      } else if (mapInfo.type === MapType.AMAP) {
        loadMaps([], () => {
          window.AMap.plugin('AMap.Geolocation', () => {
            const geolocation = new window.AMap.Geolocation({
              enableHighAccuracy: true,
              timeout: 10000
            })
 
            geolocation.getCurrentPosition((status, data) => {
              if (status === 'complete') {
                resolve({
                  coords: {
                    latitude: data.position.lat,
                    longitude: data.position.lng,
                    accuracy: data.accuracy
                  },
                  skip: true
                })
              } else {
                reject(new Error(data.message))
              }
            })
          })
        })
      } else {
        reject(new Error('network error'))
      }
    })
  }).then(({ coords, skip }) => {
    translateCoordinateSystem(type, coords, skip)
      .then(coords => {
        invoke(
          callbackId,
          Object.assign(coords, {
            errMsg: 'getLocation:ok',
            verticalAccuracy: coords.altitudeAccuracy || 0,
            // 无专门水平精度,使用位置精度替代
            horizontalAccuracy: coords.accuracy
          })
        )
      })
      .catch(error => {
        invoke(callbackId, {
          errMsg: 'getLocation:fail ' + error.message
        })
      })
  }).catch((error) => {
    invoke(callbackId, {
      errMsg: 'getLocation:fail ' + error.message || JSON.stringify(error)
    })
  })
}