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 537fa924
authored
Mar 27, 2025
by
李伟康
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
替换为百度
1 parent
6f0e04f8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
123 additions
and
98 deletions
dist/glsx-map-sdk.js
package-lock.json
package.json
src/constants.js
src/index.js
src/request.js
dist/glsx-map-sdk.js
View file @
537fa92
This diff is collapsed.
Click to expand it.
package-lock.json
View file @
537fa92
{
{
"name"
:
"glsx-map-sdk"
,
"name"
:
"glsx-map-sdk"
,
"version"
:
"0.
1
.0"
,
"version"
:
"0.
2
.0"
,
"lockfileVersion"
:
2
,
"lockfileVersion"
:
2
,
"requires"
:
true
,
"requires"
:
true
,
"packages"
:
{
"packages"
:
{
""
:
{
""
:
{
"name"
:
"glsx-map-sdk"
,
"name"
:
"glsx-map-sdk"
,
"version"
:
"0.
1
.0"
,
"version"
:
"0.
2
.0"
,
"license"
:
"ISC"
,
"license"
:
"ISC"
,
"devDependencies"
:
{
"devDependencies"
:
{
"rollup"
:
"^1.32.1"
,
"rollup"
:
"^1.32.1"
,
...
...
package.json
View file @
537fa92
{
{
"name"
:
"glsx-map-sdk"
,
"name"
:
"glsx-map-sdk"
,
"version"
:
"0.
2
.0"
,
"version"
:
"0.
3
.0"
,
"description"
:
"glsx-map-sdk"
,
"description"
:
"glsx-map-sdk"
,
"main"
:
"src/index.js"
,
"main"
:
"src/index.js"
,
"scripts"
:
{
"scripts"
:
{
...
...
src/constants.js
View file @
537fa92
...
@@ -7,4 +7,4 @@
...
@@ -7,4 +7,4 @@
* @LastEditTime: 2023-07-05 08:48:32
* @LastEditTime: 2023-07-05 08:48:32
* @FilePath: \glsx-map-sdk\src\constants.js
* @FilePath: \glsx-map-sdk\src\constants.js
*/
*/
export
const
MAP_OPTIONS
=
{
domain
:
'https://
restapi.amap.com'
,
KEY
:
'373bd88fa36ebc0247d60281386b6cec'
,
SECRET
:
'a2a93fa04721a36819d86b71cb31fe41'
}
export
const
MAP_OPTIONS
=
{
domain
:
'https://
api.map.baidu.com'
,
KEY
:
'bBk6VKyCi8pmGnnvTsSr6ULPY86JdBCV'
}
src/index.js
View file @
537fa92
...
@@ -14,6 +14,26 @@ class GlMapApi {
...
@@ -14,6 +14,26 @@ class GlMapApi {
this
.
request
=
new
Request
(
key
,
secret
)
this
.
request
=
new
Request
(
key
,
secret
)
}
}
/**
/**
* 使用 Haversine 公式计算两个坐标点之间的直线距离
* @param {Array} from - 起始位置 [纬度, 经度]
* @param {Array} to - 结束位置 [纬度, 经度]
* @returns {number} - 返回两点之间的直线距离(单位:米)
*/
calculateHaversineDistance
(
from
,
to
)
{
const
R
=
6371
e3
;
// 地球半径,单位:米
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 {*} from 起始位置 [lat,lng]
* @param {*} to 结束位置 [lat,lng]
* @param {*} to 结束位置 [lat,lng]
...
@@ -21,18 +41,32 @@ class GlMapApi {
...
@@ -21,18 +41,32 @@ 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
(
','
),
if
(
type
==
0
)
{
origins
=
JSON
.
parse
(
JSON
.
stringify
(
to
)).
reverse
().
join
(
','
),
const
distance
=
this
.
calculateHaversineDistance
(
from
,
to
)
{
results
}
=
await
this
.
request
.
get
(
'/v3/distance'
,
{
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
,
destination
,
origins
,
origin
,
type
,
...
$attrs
...
$attrs
})
})
return
{
return
{
elements
:
r
esults
elements
:
r
outes
[
0
]
}
}
}
catch
(
error
)
{
}
catch
(
error
)
{
throw
error
throw
error
...
@@ -46,16 +80,16 @@ class GlMapApi {
...
@@ -46,16 +80,16 @@ 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
distanceMatrixType
=
{
origins
=
JSON
.
parse
(
JSON
.
stringify
(
toList
))
1
:
'driving'
,
.
map
(
e
=>
(
e
=
e
.
reverse
()))
3
:
'walking'
.
join
(
'|'
),
},
origins
=
JSON
.
parse
(
JSON
.
stringify
(
from
)).
join
(
','
),
{
results
}
=
await
this
.
request
.
get
(
'/v3/distance'
,
{
destinations
=
JSON
.
parse
(
JSON
.
stringify
(
toList
)).
join
(
'|'
),
destination
,
results
=
await
this
.
request
.
get
(
`/routematrix/v2/
${
distanceMatrixType
[
type
]}
`
,
{
destinations
,
origins
,
origins
,
type
,
...
$attrs
...
$attrs
}),
}),
res
=
{
res
=
{
...
@@ -77,8 +111,8 @@ class GlMapApi {
...
@@ -77,8 +111,8 @@ 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
(
'/
geocoding/v3
'
,
{
address
,
address
,
...
$attrs
...
$attrs
})
})
...
@@ -89,17 +123,18 @@ class GlMapApi {
...
@@ -89,17 +123,18 @@ 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
result
=
await
this
.
request
.
get
(
'/reverse_geocoding/v3'
,
location
:
JSON
.
parse
(
JSON
.
stringify
(
location
)).
reverse
().
join
(
','
),
{
location
:
`
${
lat
}
,
${
lng
}
}`
,
...
$attrs
...
$attrs
})
,
})
{
addressComponent
,
formatted_address
}
=
regeocode
,
const
{
addressComponent
,
formatted_address
,
cityCode
}
=
result
address
=
`
${
addressComponent
.
province
||
''
}${
addressComponent
.
city
||
''
}${
addressComponent
.
district
||
''
}${
addressComponent
.
township
||
''
}
`
,
const
address
=
`
${
addressComponent
.
province
||
''
}${
addressComponent
.
city
||
''
}${
addressComponent
.
district
||
''
}${
addressComponent
.
town
||
''
}
`
,
data
=
{
data
=
{
...
re
geocode
,
...
re
sult
,
address
,
address
,
formatted_addresses
:
{
formatted_addresses
:
{
recommend
:
formatted_address
,
recommend
:
formatted_address
,
...
@@ -111,12 +146,12 @@ class GlMapApi {
...
@@ -111,12 +146,12 @@ class GlMapApi {
district
:
addressComponent
.
district
,
district
:
addressComponent
.
district
,
nation
:
addressComponent
.
country
,
nation
:
addressComponent
.
country
,
province
:
addressComponent
.
province
,
province
:
addressComponent
.
province
,
street
:
addressComponent
.
town
ship
street
:
addressComponent
.
town
},
},
ad_info
:
{
ad_info
:
{
adcode
:
addressComponent
.
adcode
,
adcode
:
addressComponent
.
adcode
,
city
:
addressComponent
.
city
,
city
:
addressComponent
.
city
,
city_code
:
addressComponent
.
cityc
ode
,
city_code
:
cityC
ode
,
district
:
addressComponent
.
district
,
district
:
addressComponent
.
district
,
location
:
{
location
:
{
lat
,
lat
,
...
@@ -125,7 +160,7 @@ class GlMapApi {
...
@@ -125,7 +160,7 @@ class GlMapApi {
name
:
`
${
addressComponent
.
country
+
','
||
''
}${
addressComponent
.
province
+
','
||
''
}${
addressComponent
.
city
+
','
||
''
}${
addressComponent
.
district
||
''
}
`
,
name
:
`
${
addressComponent
.
country
+
','
||
''
}${
addressComponent
.
province
+
','
||
''
}${
addressComponent
.
city
+
','
||
''
}${
addressComponent
.
district
||
''
}
`
,
nation
:
addressComponent
.
country
,
nation
:
addressComponent
.
country
,
nation_code
:
''
,
nation_code
:
''
,
phone_area_code
:
addressComponent
.
cityc
ode
,
phone_area_code
:
cityC
ode
,
province
:
addressComponent
.
province
province
:
addressComponent
.
province
},
},
location
:
{
location
:
{
...
@@ -143,29 +178,30 @@ class GlMapApi {
...
@@ -143,29 +178,30 @@ 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
(
'/
location
/ip'
,
$attrs
)
}
}
/**
/**
* 坐标转换用于将一组坐标转换为不同的坐标系
* 坐标转换用于将一组坐标转换为不同的坐标系
* @param {*} locationsList [[lat,lng],[lat,lng]] 纬度前,经度后 (最多支持40对坐标)
* @param {*} locationsList [[lat,lng],[lat,lng]] 纬度前,经度后 (最多支持40对坐标)
* @param {*} coordsys 原坐标系(可选值:
* @param {*} model 原坐标系(可选值:
gps;
转换方式可选值:
mapbar;
1:amap/tencent to bd09ll
baidu;
2:gps to bd09ll
autonavi(不进行转换))
3:bd09ll to bd09mc
* @param {*} $attrs 自定义入参 参考https://lbs.amap.com/api/webservice/gettingstarted
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
* @returns
*/
*/
translate
(
locationsList
,
coordsys
=
'autonavi'
,
$attrs
=
{})
{
translate
(
locationsList
,
model
=
1
,
$attrs
=
{})
{
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用;连接,得到locations字符串
const
locations
=
JSON
.
parse
(
JSON
.
stringify
(
locationsList
))
const
coords
=
JSON
.
parse
(
JSON
.
stringify
(
locationsList
)).
map
(
e
=>
(
e
=
e
.
reverse
()))
.
map
(
e
=>
(
e
=
e
.
reverse
()))
.
join
(
';'
)
.
join
(
'|'
)
return
this
.
request
.
get
(
'/geoconv/v2/'
,
{
// 调用request对象的get方法,传入请求路径为/v3/assistant/coordinate/convert,请求数据为locations字符串和coordsys参数(默认为autonavi),并返回结果
coords
,
return
this
.
request
.
get
(
'/v3/assistant/coordinate/convert'
,
{
model
,
locations
,
coordsys
,
...
$attrs
...
$attrs
})
})
}
}
...
@@ -178,21 +214,20 @@ class GlMapApi {
...
@@ -178,21 +214,20 @@ 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
=
{
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
{
try
{
const
{
route
}
=
await
this
.
request
.
get
(
funName
,
{
const
distanceMatrixType
=
{
origin
,
3
:
'driving'
,
destination
,
2
:
'riding'
,
...
$attrs
1
:
'walking'
})
},
origin
=
JSON
.
parse
(
JSON
.
stringify
(
from
)).
join
(
','
),
return
Promise
.
resolve
(
route
)
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
)
{
}
catch
(
error
)
{
return
Promise
.
reject
(
error
)
return
Promise
.
reject
(
error
)
}
}
...
@@ -203,14 +238,14 @@ class GlMapApi {
...
@@ -203,14 +238,14 @@ 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
(
query
,
location
=
[]
,
$attrs
=
{})
{
try
{
try
{
const
{
tips
}
=
await
this
.
request
.
get
(
'/v3/assistant/inputtips
'
,
{
const
result
=
await
this
.
request
.
get
(
'/place/v2/suggestion
'
,
{
keywords
,
query
,
location
:
JSON
.
parse
(
JSON
.
stringify
(
location
)).
reverse
().
join
(
','
),
location
:
JSON
.
parse
(
JSON
.
stringify
(
location
)).
join
(
','
),
...
$attrs
...
$attrs
})
})
return
Promise
.
resolve
(
tips
)
return
Promise
.
resolve
(
result
)
}
catch
(
error
)
{
}
catch
(
error
)
{
return
Promise
.
reject
(
error
)
return
Promise
.
reject
(
error
)
}
}
...
...
src/request.js
View file @
537fa92
...
@@ -20,22 +20,23 @@ class Request {
...
@@ -20,22 +20,23 @@ class Request {
}
}
/**对参数进行排序:按参数名升序 */
/**对参数进行排序:按参数名升序 */
setLocaleCompare
(
params
)
{
setLocaleCompare
(
params
)
{
// 将key添加到参数对象中
// 将key添加到参数对象中
params
.
key
=
this
.
key
params
.
ak
=
this
.
key
params
.
output
=
'json'
return
Object
.
fromEntries
(
Object
.
entries
(
params
).
sort
(([
a
],
[
b
])
=>
a
.
localeCompare
(
b
)))
return
Object
.
fromEntries
(
Object
.
entries
(
params
).
sort
(([
a
],
[
b
])
=>
a
.
localeCompare
(
b
)))
}
}
/**签名计算(sig) */
/**签名计算(sig) */
getSign
(
params
)
{
getSign
(
params
)
{
return
md5
(
`
${
decodeURIComponent
(
qs
.
stringify
(
this
.
setLocaleCompare
(
params
)))}${
this
.
secret
}
`
)
return
md5
(
`
${
decodeURIComponent
(
qs
.
stringify
(
this
.
setLocaleCompare
(
params
)))}${
this
.
secret
}
`
)
}
}
/**封装的GET方法 */
/**封装的GET方法 */
get
(
method
,
data
)
{
get
(
method
,
data
)
{
// 调用request方法,传入请求方式为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方法,传入请求数据和请求路径,得到签名
// 调用getSign方法,传入请求数据和请求路径,得到签名
const
sig
=
this
.
getSign
(
data
,
method
),
const
sig
=
this
.
getSign
(
data
,
method
),
obj
=
{
obj
=
{
...
@@ -45,39 +46,28 @@ class Request {
...
@@ -45,39 +46,28 @@ class Request {
if
(
isMiniprogram
)
{
if
(
isMiniprogram
)
{
wx
.
showNavigationBarLoading
()
wx
.
showNavigationBarLoading
()
try
{
try
{
const
res
=
await
new
Promise
((
resolve
,
reject
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
wx
.
request
({
wx
.
request
({
url
:
`
${
MAP_OPTIONS
.
domain
}${
method
}
`
,
url
:
`
${
MAP_OPTIONS
.
domain
}${
method
}
`
,
data
:
obj
,
data
:
obj
,
method
:
RequestMethods
,
method
:
RequestMethods
,
header
:
header
:
{
RequestMethods
===
'POST'
'content-type'
:
'application/json'
?
{
},
'content-type'
:
'application/json'
success
:
({
data
})
=>
{
}
const
{
status
,
result
}
=
data
:
{},
if
(
0
===
status
)
{
dataType
:
'json'
,
resolve
(
result
?
result
:
data
)
responseType
:
'text'
,
}
else
{
success
:
resolve
,
wx
.
showToast
({
title
:
'errMsg'
,
icon
:
'error'
})
}
},
fail
:
reject
fail
:
reject
})
})
})
})
const
{
statusCode
,
data
,
errMsg
}
=
res
if
(
statusCode
==
'200'
)
{
if
(
data
.
status
==
'1'
)
{
return
data
}
else
{
wx
.
showToast
({
title
:
data
.
info
,
icon
:
'none'
})
}
}
else
{
wx
.
showToast
({
title
:
errMsg
,
icon
:
'error'
})
}
}
catch
(
error
)
{
}
catch
(
error
)
{
wx
.
hideLoading
()
wx
.
hideLoading
()
console
.
error
(
`接口异常:`
,
error
)
console
.
error
(
`接口异常:`
,
error
)
...
...
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