Commit 71dfdded by 李伟康

增加自定义入参

1 parent f8d33e4f
...@@ -18,16 +18,18 @@ class GlMapApi { ...@@ -18,16 +18,18 @@ class GlMapApi {
* @param {*} from 起始位置 [lat,lng] * @param {*} from 起始位置 [lat,lng]
* @param {*} to 结束位置 [lat,lng] * @param {*} to 结束位置 [lat,lng]
* @param {*} type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离) * @param {*} type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns {Promise<{elements: Array}>} * @returns {Promise<{elements: Array}>}
*/ */
async calculateDistance(from, to, type = 1) { async calculateDistance (from, to, type = 1, $attrs = {}) {
try { try {
const destination = JSON.parse(JSON.stringify(from)).reverse().join(','), const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
origins = JSON.parse(JSON.stringify(to)).reverse().join(','), origins = JSON.parse(JSON.stringify(to)).reverse().join(','),
{ results } = await this.request.get('/v3/distance', { { results } = await this.request.get('/v3/distance', {
destination, destination,
origins, origins,
type type,
...$attrs
}) })
return { return {
elements: results elements: results
...@@ -41,9 +43,10 @@ class GlMapApi { ...@@ -41,9 +43,10 @@ class GlMapApi {
* @param {*} from 起始位置 [lat,lng] * @param {*} from 起始位置 [lat,lng]
* @param {*} toList 终点位置 [[lat,lng],[lat,lng]] * @param {*} toList 终点位置 [[lat,lng],[lat,lng]]
* @param {*} type 直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离) * @param {*} type 直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns {Promise<{rows: Array}>} * @returns {Promise<{rows: Array}>}
*/ */
async calculateDistanceMatrix(from, toList, type = 1) { async calculateDistanceMatrix (from, toList, type = 1, $attrs = {}) {
try { try {
const destination = JSON.parse(JSON.stringify(from)).reverse().join(','), const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
origins = JSON.parse(JSON.stringify(toList)) origins = JSON.parse(JSON.stringify(toList))
...@@ -52,7 +55,8 @@ class GlMapApi { ...@@ -52,7 +55,8 @@ class GlMapApi {
{ results } = await this.request.get('/v3/distance', { { results } = await this.request.get('/v3/distance', {
destination, destination,
origins, origins,
type type,
...$attrs
}), }),
res = { res = {
rows: [ rows: [
...@@ -70,23 +74,27 @@ class GlMapApi { ...@@ -70,23 +74,27 @@ class GlMapApi {
/** /**
* 地址解析(地址转坐标) * 地址解析(地址转坐标)
* @param {*} address (String) * @param {*} address (String)
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns * @returns
*/ */
geocoder(address) { geocoder (address, $attrs = {}) {
return this.request.get('/v3/geocode/geo', { return this.request.get('/v3/geocode/geo', {
address address,
...$attrs
}) })
} }
/** /**
* 逆地址解析(坐标位置描述) * 逆地址解析(坐标位置描述)
* @param {*} location [lat<纬度>,lng<经度>] * @param {*} location [lat<纬度>,lng<经度>]
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns * @returns
*/ */
async detailedAdrress(location) { async detailedAdrress (location, $attrs = {}) {
try { try {
const [lat, lng] = location const [lat, lng] = location
const { regeocode } = await this.request.get('/v3/geocode/regeo', { const { regeocode } = await this.request.get('/v3/geocode/regeo', {
location: JSON.parse(JSON.stringify(location)).reverse().join(',') location: JSON.parse(JSON.stringify(location)).reverse().join(','),
...$attrs
}), }),
{ addressComponent, formatted_address } = regeocode, { addressComponent, formatted_address } = regeocode,
address = `${addressComponent.province || ''}${addressComponent.city || ''}${addressComponent.district || ''}${addressComponent.township || ''}`, address = `${addressComponent.province || ''}${addressComponent.city || ''}${addressComponent.district || ''}${addressComponent.township || ''}`,
...@@ -132,10 +140,11 @@ class GlMapApi { ...@@ -132,10 +140,11 @@ class GlMapApi {
} }
/** /**
* IP定位 * IP定位
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns * @returns
*/ */
getLocationByIp() { getLocationByIp ($attrs = {}) {
return this.request.get('/v3/ip') return this.request.get('/v3/ip', $attrs)
} }
/** /**
* 坐标转换用于将一组坐标转换为不同的坐标系 * 坐标转换用于将一组坐标转换为不同的坐标系
...@@ -145,9 +154,10 @@ class GlMapApi { ...@@ -145,9 +154,10 @@ class GlMapApi {
mapbar; mapbar;
baidu; baidu;
autonavi(不进行转换)) autonavi(不进行转换))
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns * @returns
*/ */
translate(locationsList, coordsys = 'autonavi') { translate (locationsList, coordsys = 'autonavi', $attrs = {}) {
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串 // 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串
const locations = JSON.parse(JSON.stringify(locationsList)) const locations = JSON.parse(JSON.stringify(locationsList))
.map(e => (e = e.reverse())) .map(e => (e = e.reverse()))
...@@ -155,7 +165,8 @@ class GlMapApi { ...@@ -155,7 +165,8 @@ class GlMapApi {
// 调用request对象的get方法,传入请求路径为/v3/assistant/coordinate/convert,请求数据为locations字符串和coordsys参数(默认为autonavi),并返回结果 // 调用request对象的get方法,传入请求路径为/v3/assistant/coordinate/convert,请求数据为locations字符串和coordsys参数(默认为autonavi),并返回结果
return this.request.get('/v3/assistant/coordinate/convert', { return this.request.get('/v3/assistant/coordinate/convert', {
locations, locations,
coordsys coordsys,
...$attrs
}) })
} }
...@@ -164,9 +175,10 @@ class GlMapApi { ...@@ -164,9 +175,10 @@ class GlMapApi {
* @param {*} from 起始位置 [lat,lng] 纬度前,经度后 * @param {*} from 起始位置 [lat,lng] 纬度前,经度后
* @param {*} to 终点位置 [lat,lng] 纬度前,经度后 * @param {*} to 终点位置 [lat,lng] 纬度前,经度后
* @param {*} type 出行类型 1步行 2骑车 3驾车 * @param {*} type 出行类型 1步行 2骑车 3驾车
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
*/ */
async routePlanning(from, to, type = 3) { async routePlanning (from, to, type = 3, $attrs = {}) {
const funName = { const funName = {
1: '/v3/direction/walking', 1: '/v3/direction/walking',
2: '/v4/direction/bicycling', 2: '/v4/direction/bicycling',
...@@ -177,7 +189,8 @@ class GlMapApi { ...@@ -177,7 +189,8 @@ class GlMapApi {
try { try {
const { route } = await this.request.get(funName, { const { route } = await this.request.get(funName, {
origin, origin,
destination destination,
...$attrs
}) })
return Promise.resolve(route) return Promise.resolve(route)
} catch (error) { } catch (error) {
...@@ -188,12 +201,14 @@ class GlMapApi { ...@@ -188,12 +201,14 @@ class GlMapApi {
* 输入提示 * 输入提示
* @param {*} keywords 查询关键词 * @param {*} keywords 查询关键词
* @param {*} location 定位坐标 [lat,lng] 纬度前,经度后 * @param {*} location 定位坐标 [lat,lng] 纬度前,经度后
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
*/ */
async keywordSearch(keywords, location) { async keywordSearch (keywords, location, $attrs = {}) {
try { try {
const { tips } = await this.request.get('/v3/assistant/inputtips', { const { tips } = await this.request.get('/v3/assistant/inputtips', {
keywords, keywords,
location: JSON.parse(JSON.stringify(location)).reverse().join(',') location: JSON.parse(JSON.stringify(location)).reverse().join(','),
...$attrs
}) })
return Promise.resolve(tips) return Promise.resolve(tips)
} catch (error) { } catch (error) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!