Commit 6f0e04f8 by liimin

v0.2.0

1 parent 71dfdded
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
<br class="clear"> <br class="clear">
<footer> <footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Jul 05 2023 08:59:20 GMT+0800 (中国标准时间) Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Thu Jul 06 2023 14:31:29 GMT+0800 (中国标准时间)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
* @Author: liweikang * @Author: liweikang
* @Date: 2023-07-04 17:22:43 * @Date: 2023-07-04 17:22:43
* @LastEditors: limin * @LastEditors: limin
* @LastEditTime: 2023-07-05 08:53:50 * @LastEditTime: 2023-07-05 15:04:01
* @FilePath: \glsx-map-sdk\src\index.js * @FilePath: \glsx-map-sdk\src\index.js
*/ */
import Request from './request' import Request from './request'
...@@ -46,81 +46,86 @@ class GlMapApi { ...@@ -46,81 +46,86 @@ 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之间的距离)
* @returns * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns {Promise&lt;{elements: Array}>}
*/ */
async calculateDistance(from, to, type = 1) { async calculateDistance(from, to, type = 1, $attrs = {}) {
try { try {
// 使用数组解构和map方法,将from和to参数转换为字符串,并反转数组顺序,然后用逗号连接,得到目的地坐标和出发地坐标 const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
const [destination, origins] = [from, to].map(coord => JSON.stringify(coord).reverse().join(',')) origins = JSON.parse(JSON.stringify(to)).reverse().join(','),
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性 { results } = await this.request.get('/v3/distance', {
const { results } = await this.request.get('/v3/distance', { destination,
destination, origins,
origins, type,
type ...$attrs
}) })
// 返回一个包含elements属性的对象,值为results return {
return { elements: results } elements: results
}
} catch (error) { } catch (error) {
// 如果发生异常,则抛出错误对象
throw error throw error
} }
} }
/** /**
* 批量距离计算(矩阵) * 批量距离计算(矩阵)
* @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之间的距离)
* @returns * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns {Promise&lt;{rows: Array}>}
*/ */
async calculateDistanceMatrix(from, toList, type = 1) { async calculateDistanceMatrix(from, toList, type = 1, $attrs = {}) {
try { try {
// 使用数组解构和map方法,将from和toList参数转换为字符串,并反转数组顺序,然后用逗号或竖线连接,得到目的地坐标和出发地坐标 const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
const [destination, origins] = [from, toList].map(coord => origins = JSON.parse(JSON.stringify(toList))
JSON.stringify(coord) .map(e => (e = e.reverse()))
.reverse() .join('|'),
.join(coord === from ? ',' : '|') { results } = await this.request.get('/v3/distance', {
) destination,
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性 origins,
const { results } = await this.request.get('/v3/distance', { type,
destination, ...$attrs
origins, }),
type res = {
}) rows: [
// 返回一个包含rows属性的对象,值为一个包含elements属性的对象数组,值为results {
return { rows: [{ elements: results }] } elements: results
}
]
}
return Promise.resolve(res)
} catch (error) { } catch (error) {
// 如果发生异常,则抛出错误对象 return Promise.reject(error)
throw error
} }
} }
/** /**
* 地址解析(地址转坐标) * 地址解析(地址转坐标)
* @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&lt;纬度>,lng&lt;经度>] * @param {*} location [lat&lt;纬度>,lng&lt;经度>]
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns * @returns
*/ */
async detailedAdrress(location) { async detailedAdrress(location, $attrs = {}) {
try { try {
// 使用数组解构,将location参数分解为纬度和经度
const [lat, lng] = location const [lat, lng] = location
// 调用request对象的get方法,传入请求路径为/v3/geocode/regeo,请求数据为坐标字符串(反转数组顺序并用逗号连接),并等待异步结果,并解构响应数据中的regeocode属性
const { regeocode } = await this.request.get('/v3/geocode/regeo', { const { regeocode } = await this.request.get('/v3/geocode/regeo', {
location: JSON.stringify(location).reverse().join(',') location: JSON.parse(JSON.stringify(location)).reverse().join(','),
...$attrs
}), }),
// 解构regeocode对象中的addressComponent和formatted_address属性
{ 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 || ''}`,
// 定义一个返回数据对象,包含regeocode对象中的所有属性,以及一些格式化后的地址信息和坐标信息
data = { data = {
...regeocode, ...regeocode,
address, address,
...@@ -156,20 +161,18 @@ class GlMapApi { ...@@ -156,20 +161,18 @@ class GlMapApi {
lng lng
} }
} }
return Promise.resolve(data)
// 返回一个成功的Promise对象,值为返回数据对象
return data
} catch (error) { } catch (error) {
// 如果发生异常,则抛出错误对象 return Promise.reject(error)
throw error
} }
} }
/** /**
* 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)
} }
/** /**
* 坐标转换用于将一组坐标转换为不同的坐标系 * 坐标转换用于将一组坐标转换为不同的坐标系
...@@ -179,17 +182,67 @@ class GlMapApi { ...@@ -179,17 +182,67 @@ class GlMapApi {
mapbar; mapbar;
baidu; baidu;
autonavi(不进行转换)) autonavi(不进行转换))
* @returns * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns
*/ */
translate(locationsList, coordsys = 'autonavi') { translate(locationsList, coordsys = 'autonavi', $attrs = {}) {
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串 // 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串
const [locations] = [locationsList].map(coord => JSON.stringify(coord).reverse().join('|')) const locations = JSON.parse(JSON.stringify(locationsList))
.map(e => (e = e.reverse()))
.join('|')
// 调用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
}) })
} }
/**
* 路线规划
* @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 = {}) {
const funName = {
1: '/v3/direction/walking',
2: '/v4/direction/bicycling',
3: '/v3/direction/driving'
}[type],
origin = JSON.parse(JSON.stringify(from)).reverse().join(','),
destination = JSON.parse(JSON.stringify(to)).reverse().join(',')
try {
const { route } = await this.request.get(funName, {
origin,
destination,
...$attrs
})
return Promise.resolve(route)
} catch (error) {
return Promise.reject(error)
}
}
/**
* 输入提示
* @param {*} keywords 查询关键词
* @param {*} location 定位坐标 [lat,lng] 纬度前,经度后
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
*/
async keywordSearch(keywords, location, $attrs = {}) {
try {
const { tips } = await this.request.get('/v3/assistant/inputtips', {
keywords,
location: JSON.parse(JSON.stringify(location)).reverse().join(','),
...$attrs
})
return Promise.resolve(tips)
} catch (error) {
return Promise.reject(error)
}
}
} }
export default GlMapApi export default GlMapApi
</code></pre> </code></pre>
...@@ -208,7 +261,7 @@ export default GlMapApi ...@@ -208,7 +261,7 @@ export default GlMapApi
<br class="clear"> <br class="clear">
<footer> <footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Wed Jul 05 2023 08:59:20 GMT+0800 (中国标准时间) Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Thu Jul 06 2023 14:31:29 GMT+0800 (中国标准时间)
</footer> </footer>
<script> prettyPrint(); </script> <script> prettyPrint(); </script>
......
{ {
"name": "glsx-map-sdk", "name": "glsx-map-sdk",
"version": "0.1.5", "version": "0.2.0",
"description": "glsx-map-sdk", "description": "glsx-map-sdk",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"build": "rollup -c" "build": "rollup -c",
"doc":"jsdoc src/index.js"
}, },
"author": "liwk", "author": "liwk",
"license": "ISC", "license": "ISC",
......
import resolve from 'rollup-plugin-node-resolve' import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs' import commonjs from 'rollup-plugin-commonjs'
import { eslint } from 'rollup-plugin-eslint' // import { eslint } from 'rollup-plugin-eslint'
import babel from 'rollup-plugin-babel' import babel from 'rollup-plugin-babel'
import replace from 'rollup-plugin-replace' import replace from 'rollup-plugin-replace'
import { uglify } from 'rollup-plugin-uglify' import { uglify } from 'rollup-plugin-uglify'
...@@ -17,10 +17,10 @@ export default { ...@@ -17,10 +17,10 @@ export default {
plugins: [ plugins: [
resolve(), resolve(),
commonjs(), commonjs(),
eslint({ // eslint({
include: ['src/**'], // include: ['src/**'],
exclude: ['node_modules/**'] // exclude: ['node_modules/**']
}), // }),
babel({ babel({
exclude: 'node_modules/**', exclude: 'node_modules/**',
runtimeHelpers: true runtimeHelpers: true
......
...@@ -21,7 +21,7 @@ class GlMapApi { ...@@ -21,7 +21,7 @@ class GlMapApi {
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns {Promise<{elements: Array}>} * @returns {Promise<{elements: Array}>}
*/ */
async calculateDistance (from, to, type = 1, $attrs = {}) { 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(','),
...@@ -46,7 +46,7 @@ class GlMapApi { ...@@ -46,7 +46,7 @@ class GlMapApi {
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns {Promise<{rows: Array}>} * @returns {Promise<{rows: Array}>}
*/ */
async calculateDistanceMatrix (from, toList, type = 1, $attrs = {}) { 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))
...@@ -77,7 +77,7 @@ class GlMapApi { ...@@ -77,7 +77,7 @@ class GlMapApi {
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns * @returns
*/ */
geocoder (address, $attrs = {}) { geocoder(address, $attrs = {}) {
return this.request.get('/v3/geocode/geo', { return this.request.get('/v3/geocode/geo', {
address, address,
...$attrs ...$attrs
...@@ -89,13 +89,13 @@ class GlMapApi { ...@@ -89,13 +89,13 @@ class GlMapApi {
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns * @returns
*/ */
async detailedAdrress (location, $attrs = {}) { 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 ...$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 || ''}`,
data = { data = {
...@@ -143,7 +143,7 @@ class GlMapApi { ...@@ -143,7 +143,7 @@ class GlMapApi {
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns * @returns
*/ */
getLocationByIp ($attrs = {}) { getLocationByIp($attrs = {}) {
return this.request.get('/v3/ip', $attrs) return this.request.get('/v3/ip', $attrs)
} }
/** /**
...@@ -157,7 +157,7 @@ class GlMapApi { ...@@ -157,7 +157,7 @@ class GlMapApi {
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
* @returns * @returns
*/ */
translate (locationsList, coordsys = 'autonavi', $attrs = {}) { 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()))
...@@ -178,12 +178,12 @@ class GlMapApi { ...@@ -178,12 +178,12 @@ class GlMapApi {
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
*/ */
async routePlanning (from, to, type = 3, $attrs = {}) { 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',
3: '/v3/direction/driving' 3: '/v3/direction/driving'
}[type], }[type],
origin = JSON.parse(JSON.stringify(from)).reverse().join(','), origin = JSON.parse(JSON.stringify(from)).reverse().join(','),
destination = JSON.parse(JSON.stringify(to)).reverse().join(',') destination = JSON.parse(JSON.stringify(to)).reverse().join(',')
try { try {
...@@ -203,7 +203,7 @@ class GlMapApi { ...@@ -203,7 +203,7 @@ class GlMapApi {
* @param {*} location 定位坐标 [lat,lng] 纬度前,经度后 * @param {*} location 定位坐标 [lat,lng] 纬度前,经度后
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted * @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
*/ */
async keywordSearch (keywords, location, $attrs = {}) { 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,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!