Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
npm
/
glsx-map-sdk
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit a0dd9d1b
authored
Jul 04, 2023
by
liimin
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
0.0.6
1 parent
1059f5f5
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
38 deletions
dist/glsx-map-sdk.js
package.json
src/index.js
src/request.js
dist/glsx-map-sdk.js
View file @
a0dd9d1
This diff is collapsed.
Click to expand it.
package.json
View file @
a0dd9d1
{
{
"name"
:
"glsx-map-sdk"
,
"name"
:
"glsx-map-sdk"
,
"version"
:
"0.0.
5
"
,
"version"
:
"0.0.
6
"
,
"description"
:
"glsx-map-sdk"
,
"description"
:
"glsx-map-sdk"
,
"main"
:
"dist/glsx-map-sdk.js"
,
"main"
:
"dist/glsx-map-sdk.js"
,
"scripts"
:
{
"scripts"
:
{
...
@@ -11,9 +11,5 @@
...
@@ -11,9 +11,5 @@
"license"
:
"ISC"
,
"license"
:
"ISC"
,
"devDependencies"
:
{
"devDependencies"
:
{
"parcel-bundler"
:
"^1.12.5"
"parcel-bundler"
:
"^1.12.5"
},
"dependencies"
:
{
"md5"
:
"^2.3.0"
,
"qs"
:
"^6.11.2"
}
}
}
}
src/index.js
View file @
a0dd9d1
...
@@ -4,26 +4,26 @@ class GlMapApi {
...
@@ -4,26 +4,26 @@ class GlMapApi {
const
{
key
,
secret
}
=
params
const
{
key
,
secret
}
=
params
this
.
request
=
new
Request
(
key
,
secret
)
this
.
request
=
new
Request
(
key
,
secret
)
}
}
/**
计算
距离
/**
定义一个异步方法,用于计算两个坐标之间的
距离
* from 起始位置 [lat,lng]
* from 起始位置 [lat,lng]
* to 结束位置 [lat,lng]
* to 结束位置 [lat,lng]
* type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
* type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
*/
*/
async
calculateDistance
(
from
,
to
,
type
=
1
)
{
async
calculateDistance
(
from
,
to
,
type
=
1
)
{
try
{
try
{
const
destination
=
JSON
.
parse
(
JSON
.
stringify
(
from
)).
reverse
().
join
(
','
),
// 使用数组解构和map方法,将from和to参数转换为字符串,并反转数组顺序,然后用逗号连接,得到目的地坐标和出发地坐标
origins
=
JSON
.
parse
(
JSON
.
stringify
(
to
)).
reverse
().
join
(
','
),
const
[
destination
,
origins
]
=
[
from
,
to
].
map
(
coord
=>
JSON
.
stringify
(
coord
).
reverse
().
join
(
','
))
{
results
}
=
await
this
.
request
.
get
(
'/v3/distance'
,
{
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性
const
{
results
}
=
await
this
.
request
.
get
(
'/v3/distance'
,
{
destination
,
destination
,
origins
,
origins
,
type
type
}),
})
res
=
{
// 返回一个包含elements属性的对象,值为results
elements
:
results
return
{
elements
:
results
}
}
return
Promise
.
resolve
(
res
)
}
catch
(
error
)
{
}
catch
(
error
)
{
return
Promise
.
reject
(
error
)
// 如果发生异常,则抛出错误对象
throw
error
}
}
}
}
/** 批量距离计算(矩阵)
/** 批量距离计算(矩阵)
...
@@ -33,26 +33,23 @@ class GlMapApi {
...
@@ -33,26 +33,23 @@ class GlMapApi {
*/
*/
async
calculateDistanceMatrix
(
from
,
toList
,
type
=
1
)
{
async
calculateDistanceMatrix
(
from
,
toList
,
type
=
1
)
{
try
{
try
{
const
destination
=
JSON
.
parse
(
JSON
.
stringify
(
from
)).
reverse
().
join
(
','
),
// 使用数组解构和map方法,将from和toList参数转换为字符串,并反转数组顺序,然后用逗号或竖线连接,得到目的地坐标和出发地坐标
origins
=
JSON
.
parse
(
JSON
.
stringify
(
toList
))
const
[
destination
,
origins
]
=
[
from
,
toList
].
map
(
coord
=>
.
map
(
e
=>
(
e
=
e
.
reverse
()))
JSON
.
stringify
(
coord
)
.
join
(
'|'
),
.
reverse
()
{
results
}
=
await
this
.
request
.
get
(
'/v3/distance'
,
{
.
join
(
coord
===
from
?
','
:
'|'
)
)
// 调用request对象的get方法,传入请求路径为/v3/distance,请求数据为目的地坐标,出发地坐标和距离类型(默认为1),并等待异步结果,并解构响应数据中的results属性
const
{
results
}
=
await
this
.
request
.
get
(
'/v3/distance'
,
{
destination
,
destination
,
origins
,
origins
,
type
type
}),
})
res
=
{
// 返回一个包含rows属性的对象,值为一个包含elements属性的对象数组,值为results
rows
:
[
return
{
rows
:
[{
elements
:
results
}]
}
{
elements
:
results
}
]
}
return
Promise
.
resolve
(
res
)
}
catch
(
error
)
{
}
catch
(
error
)
{
return
Promise
.
reject
(
error
)
// 如果发生异常,则抛出错误对象
throw
error
}
}
}
}
/**地址解析(地址转坐标)
/**地址解析(地址转坐标)
...
@@ -67,12 +64,17 @@ class GlMapApi {
...
@@ -67,12 +64,17 @@ class GlMapApi {
* location [lat<纬度>,lng<经度>] */
* location [lat<纬度>,lng<经度>] */
async
detailedAdrress
(
location
)
{
async
detailedAdrress
(
location
)
{
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
.
parse
(
JSON
.
stringify
(
location
)
).
reverse
().
join
(
','
)
location
:
JSON
.
stringify
(
location
).
reverse
().
join
(
','
)
}),
}),
// 解构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
,
...
@@ -109,9 +111,11 @@ class GlMapApi {
...
@@ -109,9 +111,11 @@ class GlMapApi {
}
}
}
}
return
Promise
.
resolve
(
data
)
// 返回一个成功的Promise对象,值为返回数据对象
return
data
}
catch
(
error
)
{
}
catch
(
error
)
{
return
Promise
.
reject
(
error
)
// 如果发生异常,则抛出错误对象
throw
error
}
}
}
}
/**IP定位 */
/**IP定位 */
...
@@ -127,10 +131,11 @@ class GlMapApi {
...
@@ -127,10 +131,11 @@ class GlMapApi {
autonavi(不进行转换))
autonavi(不进行转换))
*
*
*/
*/
// 定义一个方法,用于将一组坐标转换为不同的坐标系
translate
(
locationsList
,
coordsys
=
'autonavi'
)
{
translate
(
locationsList
,
coordsys
=
'autonavi'
)
{
const
locations
=
JSON
.
parse
(
JSON
.
stringify
(
locationsList
))
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串
.
map
(
e
=>
(
e
=
e
.
reverse
()
))
const
[
locations
]
=
[
locationsList
].
map
(
coord
=>
JSON
.
stringify
(
coord
).
reverse
().
join
(
'|'
))
.
join
(
'|'
)
// 调用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
...
...
src/request.js
View file @
a0dd9d1
...
@@ -2,15 +2,18 @@ import md5 from 'md5'
...
@@ -2,15 +2,18 @@ import md5 from 'md5'
import
qs
from
'qs'
import
qs
from
'qs'
import
{
MAP_OPTIONS
}
from
'./constants'
import
{
MAP_OPTIONS
}
from
'./constants'
import
{
isMiniprogram
}
from
'./helper'
import
{
isMiniprogram
}
from
'./helper'
//
注释
//
定义一个Request类,用于封装请求高德地图API的方法
class
Request
{
class
Request
{
// 构造函数,接收key和secret作为参数,如果没有传入,则使用默认值
constructor
(
key
=
MAP_OPTIONS
.
KEY
,
secret
=
MAP_OPTIONS
.
SECRET
)
{
constructor
(
key
=
MAP_OPTIONS
.
KEY
,
secret
=
MAP_OPTIONS
.
SECRET
)
{
// 将key和secret赋值给实例属性
this
.
key
=
key
this
.
key
=
key
this
.
secret
=
secret
this
.
secret
=
secret
}
}
/**对参数进行排序:按参数名升序 */
/**对参数进行排序:按参数名升序 */
setLocaleCompare
(
params
)
{
setLocaleCompare
(
params
)
{
// 将key添加到参数对象中
params
.
key
=
this
.
key
params
.
key
=
this
.
key
return
Object
.
fromEntries
(
Object
.
entries
(
params
).
sort
(([
a
],
[
b
])
=>
a
.
localeCompare
(
b
)))
return
Object
.
fromEntries
(
Object
.
entries
(
params
).
sort
(([
a
],
[
b
])
=>
a
.
localeCompare
(
b
)))
}
}
...
@@ -20,10 +23,12 @@ class Request {
...
@@ -20,10 +23,12 @@ class Request {
}
}
/**封装的GET方法 */
/**封装的GET方法 */
get
(
method
,
data
)
{
get
(
method
,
data
)
{
// 调用request方法,传入请求方式为GET,请求路径为method,请求数据为data
return
this
.
request
(
'GET'
,
method
,
data
)
return
this
.
request
(
'GET'
,
method
,
data
)
}
}
/**封装的请求方法 */
/**封装的请求方法 */
async
request
(
RequestMethods
,
method
,
data
=
{})
{
async
request
(
RequestMethods
,
method
,
data
=
{})
{
// 调用getSign方法,传入请求数据和请求路径,得到签名
const
sig
=
this
.
getSign
(
data
,
method
),
const
sig
=
this
.
getSign
(
data
,
method
),
obj
=
{
obj
=
{
...
this
.
setLocaleCompare
(
data
),
...
this
.
setLocaleCompare
(
data
),
...
@@ -77,6 +82,7 @@ class Request {
...
@@ -77,6 +82,7 @@ class Request {
wx
.
hideNavigationBarLoading
()
wx
.
hideNavigationBarLoading
()
}
}
}
else
{
}
else
{
// 如果不是小程序环境,则使用window.fetch方法发送请求,并传入完整的请求地址和请求方式
const
params
=
[]
const
params
=
[]
Object
.
keys
(
obj
).
map
(
key
=>
{
Object
.
keys
(
obj
).
map
(
key
=>
{
params
.
push
(
`
${
key
}
=
${
encodeURIComponent
(
obj
[
key
])}
`
)
params
.
push
(
`
${
key
}
=
${
encodeURIComponent
(
obj
[
key
])}
`
)
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment