index.js
8.54 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* @Descripttion: api封装类
* @version: 1.0.0
* @Author: liweikang
* @Date: 2023-07-04 17:22:43
* @LastEditors: limin
* @LastEditTime: 2023-07-05 15:04:01
* @FilePath: \glsx-map-sdk\src\index.js
*/
import Request from './request'
class GlMapApi {
constructor(params = {}) {
const { key, secret } = params
this.request = new Request(key, secret)
}
/**
* 使用 Haversine 公式计算两个坐标点之间的直线距离
* @param {Array} from - 起始位置 [纬度, 经度]
* @param {Array} to - 结束位置 [纬度, 经度]
* @returns {number} - 返回两点之间的直线距离(单位:米)
*/
calculateHaversineDistance (from, to) {
const R = 6371e3; // 地球半径,单位:米
const φ1 = (from[0] * Math.PI) / 180; // 起点纬度转换为弧度
const φ2 = (to[0] * Math.PI) / 180; // 终点纬度转换为弧度
const Δφ = ((to[0] - from[0]) * Math.PI) / 180; // 纬度差
const Δλ = ((to[1] - from[1]) * Math.PI) / 180; // 经度差
const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // 距离,单位:米
}
/**
* 定义一个异步方法,用于计算两个坐标之间的距离
* @param {*} from 起始位置 [lat,lng]
* @param {*} to 结束位置 [lat,lng]
* @param {*} type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns {Promise<{elements: Array}>}
*/
async calculateDistance (from, to, type = 1, $attrs = {}) {
try {
if (type == 0) {
const distance = this.calculateHaversineDistance(from, to)
return {
elements: [
{
distance
}
]
}
}
const distanceType = {
1: 'driving',
2: 'riding',
3: 'walking'
},
destination = JSON.parse(JSON.stringify(from)).join(','),
origin = JSON.parse(JSON.stringify(to)).join(','),
{ routes } = await this.request.get(`/direction/v2/${distanceType[type]}`, {
destination,
origin,
...$attrs
})
return {
elements: routes[0]
}
} catch (error) {
throw error
}
}
/**
* 批量距离计算(矩阵)
* @param {*} from 起始位置 [lat,lng]
* @param {*} toList 终点位置 [[lat,lng],[lat,lng]]
* @param {*} type 直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns {Promise<{rows: Array}>}
*/
async calculateDistanceMatrix (from, toList, type = 1, $attrs = {}) {
try {
const distanceMatrixType = {
1: 'driving',
3: 'walking'
}, origins = JSON.parse(JSON.stringify(from)).join(','),
destinations = JSON.parse(JSON.stringify(toList)).join('|'),
results = await this.request.get(`/routematrix/v2/${distanceMatrixType[type]}`, {
destinations,
origins,
...$attrs
}),
res = {
rows: [
{
elements: results
}
]
}
return Promise.resolve(res)
} catch (error) {
return Promise.reject(error)
}
}
/**
* 地址解析(地址转坐标)
* @param {*} address (String)
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns
*/
geocoder (address, $attrs = {}) {
return this.request.get('/geocoding/v3', {
address,
...$attrs
})
}
/**
* 逆地址解析(坐标位置描述)
* @param {*} location [lat<纬度>,lng<经度>]
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns
*/
async detailedAdrress (location, $attrs = {}) {
try {
const [lat, lng] = location
const result = await this.request.get('/reverse_geocoding/v3',
{
location: `${lat},${lng}}`,
...$attrs
})
const { addressComponent, formatted_address, cityCode } = result
const address = `${addressComponent.province || ''}${addressComponent.city || ''}${addressComponent.district || ''}${addressComponent.town || ''}`,
data = {
...result,
address,
formatted_addresses: {
recommend: formatted_address,
rough: formatted_address,
standard_address: formatted_address
},
address_component: {
city: addressComponent.city,
district: addressComponent.district,
nation: addressComponent.country,
province: addressComponent.province,
street: addressComponent.town
},
ad_info: {
adcode: addressComponent.adcode,
city: addressComponent.city,
city_code: cityCode,
district: addressComponent.district,
location: {
lat,
lng
},
name: `${addressComponent.country + ',' || ''}${addressComponent.province + ',' || ''}${addressComponent.city + ',' || ''}${addressComponent.district || ''}`,
nation: addressComponent.country,
nation_code: '',
phone_area_code: cityCode,
province: addressComponent.province
},
location: {
lat,
lng
}
}
return Promise.resolve(data)
} catch (error) {
return Promise.reject(error)
}
}
/**
* IP定位
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns
*/
getLocationByIp ($attrs = {}) {
return this.request.get('/location/ip', $attrs)
}
/**
* 坐标转换用于将一组坐标转换为不同的坐标系
* @param {*} locationsList [[lat,lng],[lat,lng]] 纬度前,经度后 (最多支持40对坐标)
* @param {*} model 原坐标系(可选值:
转换方式可选值:
1:amap/tencent to bd09ll
2:gps to bd09ll
3:bd09ll to bd09mc
4:bd09mc to bd09ll
5:bd09ll to amap/tencent
6:bd09mc to amap/tencent
* @param {*} $attrs 自定义入参 参考https://lbsyun.baidu.com/faq/api?title=webapi/guide/changeposition-base
* @returns
*/
translate (locationsList, model = 1, $attrs = {}) {
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用;连接,得到locations字符串
const coords = JSON.parse(JSON.stringify(locationsList)).map(e => (e = e.reverse()))
.join(';')
return this.request.get('/geoconv/v2/', {
coords,
model,
...$attrs
})
}
/**
* 路线规划
* @param {*} from 起始位置 [lat,lng] 纬度前,经度后
* @param {*} to 终点位置 [lat,lng] 纬度前,经度后
* @param {*} type 出行类型 1步行 2骑车 3驾车
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
*/
async routePlanning (from, to, type = 3, $attrs = {}) {
try {
const distanceMatrixType = {
3: 'driving',
2: 'riding',
1: 'walking'
}, origin = JSON.parse(JSON.stringify(from)).join(','),
destination = JSON.parse(JSON.stringify(to)).join(','),
{ routes } = await this.request.get(`/direction/v2/${distanceMatrixType[type]}`, {
destination,
origin,
...$attrs
})
return Promise.resolve(routes)
} catch (error) {
return Promise.reject(error)
}
}
/**
* 输入提示
* @param {*} keywords 查询关键词
* @param {*} location 定位坐标 [lat,lng] 纬度前,经度后
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
*/
async keywordSearch (query, location = [], $attrs = {}) {
try {
const result = await this.request.get('/place/v2/suggestion', {
query,
location: JSON.parse(JSON.stringify(location)).join(','),
...$attrs
})
return Promise.resolve(result)
} catch (error) {
return Promise.reject(error)
}
}
}
export default GlMapApi