Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 位置搜索 LocationSearch 新增配置私钥 privateKey 能力 #204

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default () => {
style: 'light',
center: [120.210792, 30.246026],
zoom: 9,
// token: 'xxxx - token',
// token: '你的 token',
},
};
const APP = React.createElement(LarkMap, {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"classnames": "^2.3.1",
"color": "^4.2.3",
"lodash-es": "^4.17.21",
"md5": "^2.3.0",
"rc-select": "^14.1.13"
},
"devDependencies": {
Expand All @@ -53,6 +54,7 @@
"@types/geojson": "^7946.0.8",
"@types/jest": "^27.0.0",
"@types/lodash-es": "^4.17.6",
"@types/md5": "^2.3.5",
"@umijs/fabric": "^3.0.0",
"antd": "^4.22.3",
"dumi": "^1.1.40",
Expand Down
1 change: 0 additions & 1 deletion src/components/Control/LogoControl/demos/default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const config: LarkMapProps = {
style: 'light',
center: [120.210792, 30.246026],
zoom: 9,
// token: 'xxxx - token',
},
style: {
height: '200px',
Expand Down
4 changes: 2 additions & 2 deletions src/components/LarkMap/demos/default.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import type { LarkMapProps } from '@antv/larkmap';
import { LarkMap } from '@antv/larkmap';
import React from 'react';

const config: LarkMapProps = {
mapType: 'Gaode',
mapOptions: {
style: 'light',
center: [120.210792, 30.246026],
zoom: 9,
// token: 'xxxx - token',
// token: '你申请的 Key',
},
};

Expand Down
40 changes: 0 additions & 40 deletions src/components/LarkMap/demos/map-instance.tsx

This file was deleted.

39 changes: 38 additions & 1 deletion src/components/LarkMap/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,44 @@ LarkMap 是地图容器组件,相关地图组件与 Hooks 需放到容器组

#### 通过实例生成地图

<code src="./demos/map-instance.tsx" compact defaultShowCode></code>
```tsx | pure
import AMapLoader from '@amap/amap-jsapi-loader';
import { GaodeMap } from '@antv/l7';
import { LarkMap } from '@antv/larkmap';
import React from 'react';

/**
* 实例化
*/
// const mapInstance = new GaodeMap({
// style: 'dark',
// center: [120.210792, 30.246026],
// zoom: 10,
// // token: '你申请的 Key',
// });

/**
* 或者通过 AMapLoader
*/
const getMapInstance = () => {
return AMapLoader.load({
key: '你申请的 Key', // 申请好的 Web 端开发者 Key,首次调用 load 时必填
version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
}).then((AMap) => {
return new GaodeMap({
mapInstance: new AMap.Map('container', {
zoom: 16, //初始化地图层级
center: [116.397428, 39.90923], //初始化地图中心点
}),
});
});
};

export default () => {
return <LarkMap id="container" map={getMapInstance} style={{ height: '300px' }} />;
};
```

### API

Expand Down
7 changes: 4 additions & 3 deletions src/components/LocationSearch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ nav:

#### GaodeLocationSearchParams

| 属性值 | 描述 | 类型 | 默认值 |
| ------ | --------------------------------- | -------- | ------ |
| key | 高德 Web API 服务的 key 值 (必传) | `string` | - |
| 属性值 | 描述 | 类型 | 默认值 |
| ---------- | --------------------------------- | ------------------------- | ----------- |
| key | 高德 Web API 服务的 key 值 (必传) | `string` | - |
| privateKey | 高德 Web API 服务数字签名私钥 | `string &#124; undefined` | `undefined` |

该其他配置可以查看高德的 [输入提示](https://lbs.amap.com/api/webservice/guide/api/inputtips)

Expand Down
16 changes: 11 additions & 5 deletions src/components/LocationSearch/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useDebounceFn } from 'ahooks';
import md5 from 'md5';
import React, { useCallback, useEffect, useState } from 'react';
import { urlStringify } from '../../utils';
import { getQueryString } from '../../utils';
import { CLS_PREFIX, GAO_DE_API_URL } from './constant';
import './index.less';
import Select from './Select';
Expand Down Expand Up @@ -28,10 +29,15 @@ export const LocationSearch: React.FC<LocationSearchProps> = ({
setOptions([]);
return;
}
const url = urlStringify(GAO_DE_API_URL, {
...searchParams,
keywords: [...(searchParams.keywords ?? '').split('|'), searchText].filter((item) => !!item).join('|'),
});
const { keywords, privateKey, ...otherParams } = searchParams;
const params = {
...otherParams,
keywords: [...(keywords ?? '').split('|'), searchText].filter((item) => !!item).join('|'),
};
if (privateKey) {
params.sig = md5(getQueryString(params, false) + privateKey);
}
const url = `${GAO_DE_API_URL}?${getQueryString(params, true)}`;
const res = await (await fetch(url)).json();
setOptions(
(res?.tips ?? [])
Expand Down
1 change: 1 addition & 0 deletions src/components/LocationSearch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type GaodeLocationSearchParams = {
citylimit?: boolean;
sig?: string;
datatype?: string;
privateKey?: string;
[key: string]: any;
};

Expand Down
23 changes: 20 additions & 3 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
/**
* 将 params 参数对象转换成 query string
* @param params
* @param encode
* @returns
*/
export function getQueryString(params: Record<string, any>, encode = false) {
return Object.entries(params)
.sort(([key1], [key2]) => key1.localeCompare(key2))
.map(([key, value]) => {
let valueStr = String(value);
if (encode) {
valueStr = window.encodeURI(String(value));
}
return `${key}=${valueStr}`;
})
.join('&');
}

/**
* 将 baseUrl 和 query 拼接成 url
* @param baseUrl
* @param params
* @returns
*/
export function urlStringify(baseUrl: string, params: Record<string, any>) {
return `${baseUrl}?${Object.entries(params)
.map(([key, value]) => `${key}=${window.encodeURIComponent(String(value))}`)
.join('&')}`;
return `${baseUrl}?${getQueryString(params, true)}`;
}
Loading