Commit 601704a6 by liimin

添加doc

1 parent a0dd9d1b
......@@ -20,4 +20,3 @@ pnpm-debug.log*
*.njsproj
*.sln
*.sw?
/.cache*
No preview for this file type
This diff could not be displayed because it is too large.
No preview for this file type
No preview for this file type
This diff could not be displayed because it is too large.
No preview for this file type
No preview for this file type
This diff could not be displayed because it is too large.
No preview for this file type
No preview for this file type
This diff could not be displayed because it is too large.
No preview for this file type
No preview for this file type
This diff could not be displayed because it is too large.
No preview for this file type
No preview for this file type
This diff could not be displayed because it is too large.
No preview for this file type
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Home</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Home</h1>
<h3> </h3>
</div>
<nav>
<h2><a href="index.html">Home</a></h2>
</nav>
<br class="clear">
<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 (中国标准时间)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: index.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* @Descripttion: api封装类
* @version: 1.0.0
* @Author: liweikang
* @Date: 2023-07-04 17:22:43
* @LastEditors: limin
* @LastEditTime: 2023-07-05 08:53:50
* @FilePath: \glsx-map-sdk\src\index.js
*/
import Request from './request'
class GlMapApi {
constructor(params = {}) {
const { key, secret } = params
this.request = new Request(key, secret)
}
/**
* 定义一个异步方法,用于计算两个坐标之间的距离
* @param {*} from 起始位置 [lat,lng]
* @param {*} to 结束位置 [lat,lng]
* @param {*} type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
* @returns
*/
async calculateDistance(from, to, type = 1) {
try {
// 使用数组解构和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) {
// 如果发生异常,则抛出错误对象
throw error
}
}
/**
* 批量距离计算(矩阵)
* @param {*} from 起始位置 [lat,lng]
* @param {*} toList 重点位置 [[lat,lng],[lat,lng]]
* @param {*} type 直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
* @returns
*/
async calculateDistanceMatrix(from, toList, type = 1) {
try {
// 使用数组解构和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) {
// 如果发生异常,则抛出错误对象
throw error
}
}
/**
* 地址解析(地址转坐标)
* @param {*} address (String)
* @returns
*/
geocoder(address) {
return this.request.get('/v3/geocode/geo', {
address
})
}
/**
* 逆地址解析(坐标位置描述)
* @param {*} location [lat&lt;纬度>,lng&lt;经度>]
* @returns
*/
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.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,
formatted_addresses: {
recommend: formatted_address,
rough: formatted_address,
standard_address: formatted_address
},
address_component: {
city: addressComponent.city,
district: addressComponent.district,
nation: addressComponent.country,
province: addressComponent.province,
street: addressComponent.township
},
ad_info: {
adcode: addressComponent.adcode,
city: addressComponent.city,
city_code: addressComponent.citycode,
district: addressComponent.district,
location: {
lat,
lng
},
name: `${addressComponent.country + ',' || ''}${addressComponent.province + ',' || ''}${addressComponent.city + ',' || ''}${addressComponent.district || ''}`,
nation: addressComponent.country,
nation_code: '',
phone_area_code: addressComponent.citycode,
province: addressComponent.province
},
location: {
lat,
lng
}
}
// 返回一个成功的Promise对象,值为返回数据对象
return data
} catch (error) {
// 如果发生异常,则抛出错误对象
throw error
}
}
/**
* IP定位
* @returns
*/
getLocationByIp() {
return this.request.get('/v3/ip')
}
/**
* 坐标转换用于将一组坐标转换为不同的坐标系
* @param {*} locationsList [[lat,lng],[lat,lng]] 纬度前,经度后 (最多支持40对坐标)
* @param {*} coordsys 原坐标系(可选值:
gps;
mapbar;
baidu;
autonavi(不进行转换))
* @returns
*/
translate(locationsList, coordsys = 'autonavi') {
// 使用数组解构和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
})
}
}
export default GlMapApi
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2>
</nav>
<br class="clear">
<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 (中国标准时间)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
/*global document */
(() => {
const source = document.getElementsByClassName('prettyprint source linenums');
let i = 0;
let lineNumber = 0;
let lineId;
let lines;
let totalLines;
let anchorHash;
if (source && source[0]) {
anchorHash = document.location.hash.substring(1);
lines = source[0].getElementsByTagName('li');
totalLines = lines.length;
for (; i < totalLines; i++) {
lineNumber++;
lineId = `line${lineNumber}`;
lines[i].id = lineId;
if (lineId === anchorHash) {
lines[i].className += ' selected';
}
}
}
})();
PR.registerLangHandler(PR.createSimpleLexer([["pln",/^[\t\n\f\r ]+/,null," \t\r\n "]],[["str",/^"(?:[^\n\f\r"\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*"/,null],["str",/^'(?:[^\n\f\r'\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*'/,null],["lang-css-str",/^url\(([^"')]*)\)/i],["kwd",/^(?:url|rgb|!important|@import|@page|@media|@charset|inherit)(?=[^\w-]|$)/i,null],["lang-css-kw",/^(-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*)\s*:/i],["com",/^\/\*[^*]*\*+(?:[^*/][^*]*\*+)*\//],["com",
/^(?:<\!--|--\>)/],["lit",/^(?:\d+|\d*\.\d+)(?:%|[a-z]+)?/i],["lit",/^#[\da-f]{3,6}/i],["pln",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i],["pun",/^[^\s\w"']+/]]),["css"]);PR.registerLangHandler(PR.createSimpleLexer([],[["kwd",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i]]),["css-kw"]);PR.registerLangHandler(PR.createSimpleLexer([],[["str",/^[^"')]+/]]),["css-str"]);
@font-face {
font-family: 'Open Sans';
font-weight: normal;
font-style: normal;
src: url('../fonts/OpenSans-Regular-webfont.eot');
src:
local('Open Sans'),
local('OpenSans'),
url('../fonts/OpenSans-Regular-webfont.eot?#iefix') format('embedded-opentype'),
url('../fonts/OpenSans-Regular-webfont.woff') format('woff'),
url('../fonts/OpenSans-Regular-webfont.svg#open_sansregular') format('svg');
}
@font-face {
font-family: 'Open Sans Light';
font-weight: normal;
font-style: normal;
src: url('../fonts/OpenSans-Light-webfont.eot');
src:
local('Open Sans Light'),
local('OpenSans Light'),
url('../fonts/OpenSans-Light-webfont.eot?#iefix') format('embedded-opentype'),
url('../fonts/OpenSans-Light-webfont.woff') format('woff'),
url('../fonts/OpenSans-Light-webfont.svg#open_sanslight') format('svg');
}
html
{
overflow: auto;
background-color: #fff;
font-size: 14px;
}
body
{
font-family: 'Open Sans', sans-serif;
line-height: 1.5;
color: #4d4e53;
background-color: white;
}
a, a:visited, a:active {
color: #0095dd;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
header
{
display: block;
padding: 0px 4px;
}
tt, code, kbd, samp {
font-family: Consolas, Monaco, 'Andale Mono', monospace;
}
.class-description {
font-size: 130%;
line-height: 140%;
margin-bottom: 1em;
margin-top: 1em;
}
.class-description:empty {
margin: 0;
}
#main {
float: left;
width: 70%;
}
article dl {
margin-bottom: 40px;
}
article img {
max-width: 100%;
}
section
{
display: block;
background-color: #fff;
padding: 12px 24px;
border-bottom: 1px solid #ccc;
margin-right: 30px;
}
.variation {
display: none;
}
.signature-attributes {
font-size: 60%;
color: #aaa;
font-style: italic;
font-weight: lighter;
}
nav
{
display: block;
float: right;
margin-top: 28px;
width: 30%;
box-sizing: border-box;
border-left: 1px solid #ccc;
padding-left: 16px;
}
nav ul {
font-family: 'Lucida Grande', 'Lucida Sans Unicode', arial, sans-serif;
font-size: 100%;
line-height: 17px;
padding: 0;
margin: 0;
list-style-type: none;
}
nav ul a, nav ul a:visited, nav ul a:active {
font-family: Consolas, Monaco, 'Andale Mono', monospace;
line-height: 18px;
color: #4D4E53;
}
nav h3 {
margin-top: 12px;
}
nav li {
margin-top: 6px;
}
footer {
display: block;
padding: 6px;
margin-top: 12px;
font-style: italic;
font-size: 90%;
}
h1, h2, h3, h4 {
font-weight: 200;
margin: 0;
}
h1
{
font-family: 'Open Sans Light', sans-serif;
font-size: 48px;
letter-spacing: -2px;
margin: 12px 24px 20px;
}
h2, h3.subsection-title
{
font-size: 30px;
font-weight: 700;
letter-spacing: -1px;
margin-bottom: 12px;
}
h3
{
font-size: 24px;
letter-spacing: -0.5px;
margin-bottom: 12px;
}
h4
{
font-size: 18px;
letter-spacing: -0.33px;
margin-bottom: 12px;
color: #4d4e53;
}
h5, .container-overview .subsection-title
{
font-size: 120%;
font-weight: bold;
letter-spacing: -0.01em;
margin: 8px 0 3px 0;
}
h6
{
font-size: 100%;
letter-spacing: -0.01em;
margin: 6px 0 3px 0;
font-style: italic;
}
table
{
border-spacing: 0;
border: 0;
border-collapse: collapse;
}
td, th
{
border: 1px solid #ddd;
margin: 0px;
text-align: left;
vertical-align: top;
padding: 4px 6px;
display: table-cell;
}
thead tr
{
background-color: #ddd;
font-weight: bold;
}
th { border-right: 1px solid #aaa; }
tr > th:last-child { border-right: 1px solid #ddd; }
.ancestors, .attribs { color: #999; }
.ancestors a, .attribs a
{
color: #999 !important;
text-decoration: none;
}
.clear
{
clear: both;
}
.important
{
font-weight: bold;
color: #950B02;
}
.yes-def {
text-indent: -1000px;
}
.type-signature {
color: #aaa;
}
.name, .signature {
font-family: Consolas, Monaco, 'Andale Mono', monospace;
}
.details { margin-top: 14px; border-left: 2px solid #DDD; }
.details dt { width: 120px; float: left; padding-left: 10px; padding-top: 6px; }
.details dd { margin-left: 70px; }
.details ul { margin: 0; }
.details ul { list-style-type: none; }
.details li { margin-left: 30px; padding-top: 6px; }
.details pre.prettyprint { margin: 0 }
.details .object-value { padding-top: 0; }
.description {
margin-bottom: 1em;
margin-top: 1em;
}
.code-caption
{
font-style: italic;
font-size: 107%;
margin: 0;
}
.source
{
border: 1px solid #ddd;
width: 80%;
overflow: auto;
}
.prettyprint.source {
width: inherit;
}
.source code
{
font-size: 100%;
line-height: 18px;
display: block;
padding: 4px 12px;
margin: 0;
background-color: #fff;
color: #4D4E53;
}
.prettyprint code span.line
{
display: inline-block;
}
.prettyprint.linenums
{
padding-left: 70px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.prettyprint.linenums ol
{
padding-left: 0;
}
.prettyprint.linenums li
{
border-left: 3px #ddd solid;
}
.prettyprint.linenums li.selected,
.prettyprint.linenums li.selected *
{
background-color: lightyellow;
}
.prettyprint.linenums li *
{
-webkit-user-select: text;
-moz-user-select: text;
-ms-user-select: text;
user-select: text;
}
.params .name, .props .name, .name code {
color: #4D4E53;
font-family: Consolas, Monaco, 'Andale Mono', monospace;
font-size: 100%;
}
.params td.description > p:first-child,
.props td.description > p:first-child
{
margin-top: 0;
padding-top: 0;
}
.params td.description > p:last-child,
.props td.description > p:last-child
{
margin-bottom: 0;
padding-bottom: 0;
}
.disabled {
color: #454545;
}
/* JSDoc prettify.js theme */
/* plain text */
.pln {
color: #000000;
font-weight: normal;
font-style: normal;
}
/* string content */
.str {
color: #006400;
font-weight: normal;
font-style: normal;
}
/* a keyword */
.kwd {
color: #000000;
font-weight: bold;
font-style: normal;
}
/* a comment */
.com {
font-weight: normal;
font-style: italic;
}
/* a type name */
.typ {
color: #000000;
font-weight: normal;
font-style: normal;
}
/* a literal value */
.lit {
color: #006400;
font-weight: normal;
font-style: normal;
}
/* punctuation */
.pun {
color: #000000;
font-weight: bold;
font-style: normal;
}
/* lisp open bracket */
.opn {
color: #000000;
font-weight: bold;
font-style: normal;
}
/* lisp close bracket */
.clo {
color: #000000;
font-weight: bold;
font-style: normal;
}
/* a markup tag name */
.tag {
color: #006400;
font-weight: normal;
font-style: normal;
}
/* a markup attribute name */
.atn {
color: #006400;
font-weight: normal;
font-style: normal;
}
/* a markup attribute value */
.atv {
color: #006400;
font-weight: normal;
font-style: normal;
}
/* a declaration */
.dec {
color: #000000;
font-weight: bold;
font-style: normal;
}
/* a variable name */
.var {
color: #000000;
font-weight: normal;
font-style: normal;
}
/* a function name */
.fun {
color: #000000;
font-weight: bold;
font-style: normal;
}
/* Specify class=linenums on a pre to get line numbering */
ol.linenums {
margin-top: 0;
margin-bottom: 0;
}
/* Tomorrow Theme */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */
/* Pretty printing styles. Used with prettify.js. */
/* SPAN elements with the classes below are added by prettyprint. */
/* plain text */
.pln {
color: #4d4d4c; }
@media screen {
/* string content */
.str {
color: #718c00; }
/* a keyword */
.kwd {
color: #8959a8; }
/* a comment */
.com {
color: #8e908c; }
/* a type name */
.typ {
color: #4271ae; }
/* a literal value */
.lit {
color: #f5871f; }
/* punctuation */
.pun {
color: #4d4d4c; }
/* lisp open bracket */
.opn {
color: #4d4d4c; }
/* lisp close bracket */
.clo {
color: #4d4d4c; }
/* a markup tag name */
.tag {
color: #c82829; }
/* a markup attribute name */
.atn {
color: #f5871f; }
/* a markup attribute value */
.atv {
color: #3e999f; }
/* a declaration */
.dec {
color: #f5871f; }
/* a variable name */
.var {
color: #c82829; }
/* a function name */
.fun {
color: #4271ae; } }
/* Use higher contrast and text-weight for printable form. */
@media print, projection {
.str {
color: #060; }
.kwd {
color: #006;
font-weight: bold; }
.com {
color: #600;
font-style: italic; }
.typ {
color: #404;
font-weight: bold; }
.lit {
color: #044; }
.pun, .opn, .clo {
color: #440; }
.tag {
color: #006;
font-weight: bold; }
.atn {
color: #404; }
.atv {
color: #060; } }
/* Style */
/*
pre.prettyprint {
background: white;
font-family: Consolas, Monaco, 'Andale Mono', monospace;
font-size: 12px;
line-height: 1.5;
border: 1px solid #ccc;
padding: 10px; }
*/
/* Specify class=linenums on a pre to get line numbering */
ol.linenums {
margin-top: 0;
margin-bottom: 0; }
/* IE indents via margin-left */
li.L0,
li.L1,
li.L2,
li.L3,
li.L4,
li.L5,
li.L6,
li.L7,
li.L8,
li.L9 {
/* */ }
/* Alternate shading for lines */
li.L1,
li.L3,
li.L5,
li.L7,
li.L9 {
/* */ }
{
"name": "glsx-map-sdk",
"version": "0.0.6",
"version": "0.0.7",
"description": "glsx-map-sdk",
"main": "dist/glsx-map-sdk.js",
"scripts": {
......
/*
* @Descripttion: 常量类
* @version: 1.0.0
* @Author: limin
* @Date: 2023-07-04 17:40:51
* @LastEditors: limin
* @LastEditTime: 2023-07-05 08:48:32
* @FilePath: \glsx-map-sdk\src\constants.js
*/
export const MAP_OPTIONS = { domain: 'https://restapi.amap.com', KEY: '373bd88fa36ebc0247d60281386b6cec', SECRET: 'a2a93fa04721a36819d86b71cb31fe41' }
/*
* @Descripttion:帮助类
* @version: 1.0.0
* @Author: limin
* @Date: 2023-07-04 17:37:45
* @LastEditors: limin
* @LastEditTime: 2023-07-05 08:46:52
* @FilePath: \glsx-map-sdk\src\helper.js
*/
export const isMiniprogram = !window
/*
* @Descripttion: api封装类
* @version: 1.0.0
* @Author: liweikang
* @Date: 2023-07-04 17:22:43
* @LastEditors: limin
* @LastEditTime: 2023-07-05 09:06:58
* @FilePath: \glsx-map-sdk\src\index.js
*/
import Request from './request'
class GlMapApi {
constructor(params = {}) {
const { key, secret } = params
this.request = new Request(key, secret)
}
/** 定义一个异步方法,用于计算两个坐标之间的距离
* from 起始位置 [lat,lng]
* to 结束位置 [lat,lng]
* type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
/**
* 定义一个异步方法,用于计算两个坐标之间的距离
* @param {*} from 起始位置 [lat,lng]
* @param {*} to 结束位置 [lat,lng]
* @param {*} type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
* @returns {Promise<{elements: Array}>}
*/
async calculateDistance(from, to, type = 1) {
try {
......@@ -26,10 +37,12 @@ class GlMapApi {
throw error
}
}
/** 批量距离计算(矩阵)
* from 起始位置 [lat,lng]
* toList 重点位置 [[lat,lng],[lat,lng]]
* type 0:直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
/**
* 批量距离计算(矩阵)
* @param {*} from 起始位置 [lat,lng]
* @param {*} toList 重点位置 [[lat,lng],[lat,lng]]
* @param {*} type 直线距离 , 1:驾车导航距离(仅支持国内坐标) , 3:步行规划距离(仅支持5km之间的距离)
* @returns {Promise<{rows: Array}>}
*/
async calculateDistanceMatrix(from, toList, type = 1) {
try {
......@@ -52,16 +65,21 @@ class GlMapApi {
throw error
}
}
/**地址解析(地址转坐标)
* address (String)
/**
* 地址解析(地址转坐标)
* @param {*} address (String)
* @returns
*/
geocoder(address) {
return this.request.get('/v3/geocode/geo', {
address
})
}
/**逆地址解析(坐标位置描述)
* location [lat<纬度>,lng<经度>] */
/**
* 逆地址解析(坐标位置描述)
* @param {*} location [lat<纬度>,lng<经度>]
* @returns
*/
async detailedAdrress(location) {
try {
// 使用数组解构,将location参数分解为纬度和经度
......@@ -118,20 +136,23 @@ class GlMapApi {
throw error
}
}
/**IP定位 */
/**
* IP定位
* @returns
*/
getLocationByIp() {
return this.request.get('/v3/ip')
}
/**坐标转换
* locationsList:[[lat,lng],[lat,lng]] 纬度前,经度后 (最多支持40对坐标)
* coordsys 原坐标系(可选值:
gps;
mapbar;
baidu;
autonavi(不进行转换))
*
/**
* 坐标转换用于将一组坐标转换为不同的坐标系
* @param {*} locationsList [[lat,lng],[lat,lng]] 纬度前,经度后 (最多支持40对坐标)
* @param {*} coordsys 原坐标系(可选值:
gps;
mapbar;
baidu;
autonavi(不进行转换))
* @returns
*/
// 定义一个方法,用于将一组坐标转换为不同的坐标系
translate(locationsList, coordsys = 'autonavi') {
// 使用数组解构和map方法,将locationsList参数转换为字符串,并反转数组顺序,然后用竖线连接,得到locations字符串
const [locations] = [locationsList].map(coord => JSON.stringify(coord).reverse().join('|'))
......
/*
* @Descripttion: 定义一个Request类,用于封装请求高德地图API的方法
* @version: 1.0.0
* @Author: limin
* @Date: 2023-07-04 17:25:31
* @LastEditors: limin
* @LastEditTime: 2023-07-05 08:48:04
* @FilePath: \glsx-map-sdk\src\request.js
*/
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) {
......@@ -13,35 +21,41 @@ class Request {
/**对参数进行排序:按参数名升序 */
setLocaleCompare(params) {
// 将key添加到参数对象中
params.key = this.key
return Object.fromEntries(Object.entries(params).sort(([a], [b]) => a.localeCompare(b)))
// 使用Object.assign和展开运算符,将key添加到参数对象中,并返回一个新的对象
return Object.fromEntries(Object.entries(Object.assign({ key: this.key }, params)).sort(([a], [b]) => a.localeCompare(b)))
}
/**签名计算(sig) */
getSign(params) {
return md5(`${decodeURIComponent(qs.stringify(this.setLocaleCompare(params)))}${this.secret}`)
return md5(decodeURIComponent(qs.stringify(this.setLocaleCompare(params))) + this.secret)
}
/**封装的GET方法 */
get(method, data) {
// 调用request方法,传入请求方式为GET,请求路径为method,请求数据为data
return this.request('GET', method, data)
}
/**封装的请求方法 */
/**定义一个异步方法,用于发送请求到高德地图API */
async request(RequestMethods, method, data = {}) {
// 调用getSign方法,传入请求数据和请求路径,得到签名
const sig = this.getSign(data, method),
// 将签名添加到请求数据中,并对请求数据进行排序
obj = {
...this.setLocaleCompare(data),
sig
}
// 判断是否是小程序环境
if (isMiniprogram) {
// 显示导航栏加载动画
wx.showNavigationBarLoading()
try {
// 使用Promise封装wx.request方法,返回一个异步结果
const res = await new Promise((resolve, reject) => {
wx.request({
url: `${MAP_OPTIONS.domain}${method}`,
// 设置请求数据
data: obj,
// 设置请求方式
method: RequestMethods,
// 设置请求头,如果是POST请求,则设置content-type为application/json
header:
RequestMethods === 'POST'
? {
......@@ -54,6 +68,7 @@ class Request {
fail: reject
})
})
// 解构响应结果中的状态码,数据和错误信息
const { statusCode, data, errMsg } = res
if (statusCode == '200') {
if (data.status == '1') {
......@@ -79,16 +94,16 @@ class Request {
})
throw error
} finally {
// 最后,隐藏导航栏加载动画
wx.hideNavigationBarLoading()
}
} else {
// 如果不是小程序环境,则使用window.fetch方法发送请求,并传入完整的请求地址和请求方式
const params = []
Object.keys(obj).map(key => {
params.push(`${key}=${encodeURIComponent(obj[key])}`)
})
try {
const res = await window.fetch(`${MAP_OPTIONS.domain}${method}?${params.join('&')}`, {
// 使用qs.stringify将请求数据转换为查询字符串,并使用encodeURIComponent进行编码,然后拼接到请求地址后面
const url = `${MAP_OPTIONS.domain}${method}?${encodeURIComponent(qs.stringify(obj))}`
// 使用await等待异步结果,并解构响应对象
const res = await window.fetch(url, {
method: RequestMethods
})
const data = await res.json()
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!