Skip to content

Commit

Permalink
feat(AMapMap): 自定义地图样式
Browse files Browse the repository at this point in the history
  • Loading branch information
xyy94813 committed Nov 8, 2023
1 parent 32e87ad commit 2262565
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/components/AMapMap/AMapMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const AMapMap = forwardRef<any, PropsWithChildren<AMapMapProps>>(
cityName,
zoom,
features,
mapStyle,
}, ref) => {
const { __AMAP__: AMap } = useAMapAPI();

Expand Down Expand Up @@ -95,6 +96,8 @@ const AMapMap = forwardRef<any, PropsWithChildren<AMapMapProps>>(

useImperativeHandle(ref, () => curMap, [curMap]);

useSetter<Parameters<AMap.Map['setMapStyle']>>(curMap, 'setMapStyle', mapStyle!);

// set city
useEffect(() => {
if (cityName) {
Expand Down
6 changes: 6 additions & 0 deletions src/components/AMapMap/__tests__/AMapMap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const mockMapInstance = {
setZoom: jest.fn(),
setCenter: jest.fn(),
setFeatures: jest.fn(),
setMapStyle: jest.fn(),
on: jest.fn(),
};

Expand Down Expand Up @@ -113,4 +114,9 @@ describe('AMapMap', () => {
render(<AMapMap features={customFeatures} />);
expect(mockMapInstance.setFeatures).toBeCalledWith(customFeatures);
});

test('sets custom map style', () => {
render(<AMapMap mapStyle="amap://styles/grey" />);
expect(mockMapInstance.setMapStyle).toBeCalledWith('amap://styles/grey');
});
});
1 change: 1 addition & 0 deletions src/components/AMapMap/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export type AMapMapProps = {
cityName?: string;
zoom?: number;
features?: AMap.MapOptions['features'];
mapStyle?: AMap.MapOptions['mapStyle'];
};
17 changes: 17 additions & 0 deletions src/components/AMapMap/stories/AMapMap.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ export default {
},
},
},
mapStyle: {
description: [
'设置地图样式:',
'官方样式模版,如 "amap://styles/grey"',
'地图自定义平台定制地图样式,如 "amap://styles/d6bf8c1d69cea9f5c696185ad4ac4c86"',
].join('<br/>'),
table: {
type: { summary: 'string' },
},
control: 'text',
},
},
} as Meta;

Expand Down Expand Up @@ -85,3 +96,9 @@ SetFeatures.args = {
features: ['bg', 'point'],
};
SetFeatures.storyName = '设置地图上显示的元素种类';

export const setMapStyle: typeof Template = Template.bind({});
setMapStyle.args = {
mapStyle: 'amap://styles/grey',
};
setMapStyle.storyName = '自定义地图样式';

0 comments on commit 2262565

Please sign in to comment.