Commit a0dd9d1b by liimin

0.0.6

1 parent 1059f5f5
{
"name": "glsx-map-sdk",
"version": "0.0.5",
"version": "0.0.6",
"description": "glsx-map-sdk",
"main": "dist/glsx-map-sdk.js",
"scripts": {
......@@ -11,9 +11,5 @@
"license": "ISC",
"devDependencies": {
"parcel-bundler": "^1.12.5"
},
"dependencies": {
"md5": "^2.3.0",
"qs": "^6.11.2"
}
}
......@@ -4,26 +4,26 @@ class GlMapApi {
const { key, secret } = params
this.request = new Request(key, secret)
}
/**计算距离
/** 定义一个异步方法,用于计算两个坐标之间的距离
* from 起始位置 [lat,lng]
* to 结束位置 [lat,lng]
* type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
*/
async calculateDistance(from, to, type = 1) {
try {
const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
origins = JSON.parse(JSON.stringify(to)).reverse().join(','),
{ results } = await this.request.get('/v3/distance', {
destination,
origins,
type
}),
res = {
elements: results
}
return Promise.resolve(res)
// 使用数组解构和map方法,将from和to参数转换为字符串,并反转数组顺序,然后用逗号连接,得到目的地坐标和出发地坐标
const [destination, origins] = [from, to].map(coord => JSON.stringify(coord).reverse().join(','))
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性
const { results } = await this.request.get('/v3/distance', {
destination,
origins,
type
})
// 返回一个包含elements属性的对象,值为results
return { elements: results }
} catch (error) {
return Promise.reject(error)
// 如果发生异常,则抛出错误对象
throw error
}
}
/** 批量距离计算(矩阵)
......@@ -33,26 +33,23 @@ class GlMapApi {
*/
async calculateDistanceMatrix(from, toList, type = 1) {
try {
const destination = JSON.parse(JSON.stringify(from)).reverse().join(','),
origins = JSON.parse(JSON.stringify(toList))
.map(e => (e = e.reverse()))
.join('|'),
{ results } = await this.request.get('/v3/distance', {
destination,
origins,
type
}),
res = {
rows: [
{
elements: results
}
]
}
return Promise.resolve(res)
// 使用数组解构和map方法,将from和toList参数转换为字符串,并反转数组顺序,然后用逗号或竖线连接,得到目的地坐标和出发地坐标
const [destination, origins] = [from, toList].map(coord =>
JSON.stringify(coord)
.reverse()
.join(coord === from ? ',' : '|')
)
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性
const { results } = await this.request.get('/v3/distance', {
destination,
origins,
type
})
// 返回一个包含rows属性的对象,值为一个包含elements属性的对象数组,值为results
return { rows: [{ elements: results }] }
} catch (error) {
return Promise.reject(error)
// 如果发生异常,则抛出错误对象
throw error
}
}
/**地址解析(地址转坐标)
......@@ -67,12 +64,17 @@ class GlMapApi {
* location [lat<纬度>,lng<经度>] */
async detailedAdrress(location) {
try {
// 使用数组解构,将location参数分解为纬度和经度
const [lat, lng] = location
// 调用request对象的get方法,传入请求路径为/v3/geocode/regeo,请求数据为坐标字符串(反转数组顺序并用逗号连接),并等待异步结果,并解构响应数据中的regeocode属性
const { regeocode } = await this.request.get('/v3/geocode/regeo', {
location: JSON.parse(JSON.stringify(location)).reverse().join(',')
location: JSON.stringify(location).reverse().join(',')
}),
// 解构regeocode对象中的addressComponent和formatted_address属性
{ addressComponent, formatted_address } = regeocode,
// 拼接省市区乡镇作为地址
address = `${addressComponent.province || ''}${addressComponent.city || ''}${addressComponent.district || ''}${addressComponent.township || ''}`,
// 定义一个返回数据对象,包含regeocode对象中的所有属性,以及一些格式化后的地址信息和坐标信息
data = {
...regeocode,
address,
......@@ -109,9 +111,11 @@ class GlMapApi {
}
}
return Promise.resolve(data)
// 返回一个成功的Promise对象,值为返回数据对象
return data
} catch (error) {
return Promise.reject(error)
// 如果发生异常,则抛出错误对象
throw error
}
}
/**IP定位 */
......@@ -127,10 +131,11 @@ class GlMapApi {
autonavi(不进行转换))
*
*/
// 定义一个方法,用于将一组坐标转换为不同的坐标系
translate(locationsList, coordsys = 'autonavi') {
const locations = JSON.parse(JSON.stringify(locationsList))
.map(e => (e = e.reverse()))
.join('|')
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串
const [locations] = [locationsList].map(coord => JSON.stringify(coord).reverse().join('|'))
// 调用request对象的get方法,传入请求路径为/v3/assistant/coordinate/convert,请求数据为locations字符串和coordsys参数(默认为autonavi),并返回结果
return this.request.get('/v3/assistant/coordinate/convert', {
locations,
coordsys
......
......@@ -2,15 +2,18 @@ import md5 from 'md5'
import qs from 'qs'
import { MAP_OPTIONS } from './constants'
import { isMiniprogram } from './helper'
//注释
// 定义一个Request类,用于封装请求高德地图API的方法
class Request {
// 构造函数,接收key和secret作为参数,如果没有传入,则使用默认值
constructor(key = MAP_OPTIONS.KEY, secret = MAP_OPTIONS.SECRET) {
// 将key和secret赋值给实例属性
this.key = key
this.secret = secret
}
/**对参数进行排序:按参数名升序 */
setLocaleCompare(params) {
// 将key添加到参数对象中
params.key = this.key
return Object.fromEntries(Object.entries(params).sort(([a], [b]) => a.localeCompare(b)))
}
......@@ -20,10 +23,12 @@ class Request {
}
/**封装的GET方法 */
get(method, data) {
// 调用request方法,传入请求方式为GET,请求路径为method,请求数据为data
return this.request('GET', method, data)
}
/**封装的请求方法 */
async request(RequestMethods, method, data = {}) {
// 调用getSign方法,传入请求数据和请求路径,得到签名
const sig = this.getSign(data, method),
obj = {
...this.setLocaleCompare(data),
......@@ -77,6 +82,7 @@ class Request {
wx.hideNavigationBarLoading()
}
} else {
// 如果不是小程序环境,则使用window.fetch方法发送请求,并传入完整的请求地址和请求方式
const params = []
Object.keys(obj).map(key => {
params.push(`${key}=${encodeURIComponent(obj[key])}`)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!