Skip to content

Commit

Permalink
feat: add WorldView
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxingkang committed Jan 19, 2024
1 parent 62a26e3 commit 9f825eb
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 3 deletions.
43 changes: 43 additions & 0 deletions src/components/WorldView/WorldView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { useEffect } from 'react';
import { useMap } from '../../hooks/useMap';

import type { Code, WorldViewProps } from './types';

export const WorldView: React.FC<WorldViewProps> = (props) => {
const { code = 'CN' } = props;
const { map } = useMap();

useEffect(() => {
if (code) {
filterLayers(code);
}
}, [code]);

const filterLayers = (worldview: Code) => {
console.log(worldview);
map.setFilter('admin-0-boundary-disputed', [
'all',
['==', ['get', 'disputed'], 'true'],
['==', ['get', 'admin_level'], 0],
['==', ['get', 'maritime'], 'false'],
['match', ['get', 'worldview'], ['all', worldview], true, false],
]);

map.setFilter('admin-0-boundary', [
'all',
['==', ['get', 'admin_level'], 0],
['==', ['get', 'disputed'], 'false'],
['==', ['get', 'maritime'], 'false'],
['match', ['get', 'worldview'], ['all', worldview], true, false],
]);

map.setFilter('admin-0-boundary-bg', [
'all',
['==', ['get', 'admin_level'], 0],
['==', ['get', 'maritime'], 'false'],
['match', ['get', 'worldview'], ['all', worldview], true, false],
]);
};

return null;
};
2 changes: 2 additions & 0 deletions src/components/WorldView/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { WorldView } from './WorldView';
export type { WorldViewProps, Code } from './types';
5 changes: 5 additions & 0 deletions src/components/WorldView/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Code = 'CN' | 'IN' | 'JP' | 'US';

export interface WorldViewProps {
code?: Code;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from './components/StyleLoadFinish';
export * from './components/Source';
export * from './components/Layer';
export * from './components/MarkerCluster';
export * from './components/WorldView';
export * from './hooks/useMap';
70 changes: 70 additions & 0 deletions stories/examples/WorldView.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React, { useState } from 'react';
import { Radio } from 'antd';
import { Map, StyleLoadFinish, WorldView as WorldViewCom } from '../../src';
import { Language } from './components/Language';
import { Flyto } from './components/Flyto';

import type { Meta, StoryObj } from '@storybook/react';

const worldViews = [
{
value: 'CN',
label: '中国',
},
{
value: 'IN',
label: '印度',
},
{
value: 'JP',
label: '日本',
},
{
value: 'US',
label: '美国',
},
];

const meta = {
title: '示例/WorldView',
render: () => {
const [worldView, setWorldView] = useState(worldViews[0].value);

return (
<Map
containerStyle={{ height: '100vh' }}
zoom={1}
style="mapbox://styles/mapbox/light-v11"
onClick={(e) => {
console.log(e);
}}
>
<StyleLoadFinish>
<Flyto center={[85.14477595859074, 29.68859973130745]} zoom={4} />
<WorldViewCom code={worldView as any} />
</StyleLoadFinish>
<Language />

<div style={{ position: 'absolute', background: '#efefef', right: 0, padding: 12 }}>
<Radio.Group
value={worldView}
options={worldViews}
onChange={(e) => {
setWorldView(e.target.value);
}}
/>
</div>
</Map>
);
},
parameters: {},
argTypes: {},
args: {},
} satisfies Meta<typeof Map>;

export default meta;
type Story = StoryObj<typeof meta>;

export const WorldView: Story = {
args: {},
};
12 changes: 9 additions & 3 deletions stories/examples/components/Flyto.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import React, { useEffect } from 'react';
import { useMap } from '../../../src';

export const Flyto: React.FC = () => {
interface FlytoProps {
center?: [number, number];
zoom?: number;
}

export const Flyto: React.FC<FlytoProps> = (props) => {
const { center = [116.36904455256712, 39.94939362625391], zoom = 12.5 } = props;
const { map } = useMap();

useEffect(() => {
if (map) {
map.flyTo({
center: [116.36904455256712, 39.94939362625391],
zoom: 12.5,
center,
zoom,
duration: 12 * 1000,
essential: true,
});
Expand Down

0 comments on commit 9f825eb

Please sign in to comment.