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: map support update #51

Merged
merged 5 commits into from
Dec 9, 2024
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
5 changes: 5 additions & 0 deletions .changeset/strange-turtles-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@antv/gpt-vis': patch
---

feat: map support update
20 changes: 20 additions & 0 deletions src/Map/Component/MapView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useScene } from '@antv/larkmap';
import { useEffect } from 'react';
import type { Map } from '../../types/map';
import { fitBounds, setMapStatus, setMapView } from '../../utils/map';

// 更新地图视野
export default (props: Map) => {
const scene = useScene();
useEffect(() => {
setMapView(props, scene);
}, []);
useEffect(() => {
setMapStatus(props, scene);
}, [props.enableRotate, props.enableScroll, props.enableZoom]);
useEffect(() => {
fitBounds(props, scene);
}, [props.includePoints, props.markers, props.polyline, props.includePadding]);

return null;
};
33 changes: 33 additions & 0 deletions src/Map/Component/Marker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { ILayer } from '@antv/l7';
import { useScene } from '@antv/larkmap';
import { useEffect, useState } from 'react';
import type { Map } from '../../types/map';
import { setMapContext, setMarkers } from '../../utils/map';
// 渲染标记点
export default (props: Map) => {
const scene = useScene();
const [layers, setLayers] = useState<ILayer[]>([]);
const removeLayers = () => {
layers.forEach((item) => {
scene.removeLayer(item);
});
};
useEffect(() => {
if (!props.markers) return;
// 异步调用
setMapContext(props, scene)?.then(() => {
// 初始化资源
const markerLayer = setMarkers(props.markers || []);
removeLayers();
markerLayer.forEach((item) => {
scene.addLayer(item);
});
setLayers(markerLayer);
});
return () => {
removeLayers();
};
}, [props.markers]);

return null;
};
30 changes: 30 additions & 0 deletions src/Map/Component/Polyline.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ILayer } from '@antv/l7';
import { useScene } from '@antv/larkmap';
import { useEffect, useState } from 'react';
import type { Map } from '../../types/map';
import { setPolyline } from '../../utils/map';
// 渲染线图层
export default (props: Map) => {
const scene = useScene();
const [layers, setLayers] = useState<ILayer[]>([]);
const removeLayers = () => {
layers.forEach((item) => {
scene.removeLayer(item);
});
};
useEffect(() => {
if (!props.polyline) return;
const lineLayers = setPolyline(props.polyline || []);
removeLayers();
lineLayers.forEach((item) => {
scene.addLayer(item);
});
setLayers(lineLayers);

return () => {
removeLayers();
};
}, [props.polyline]);

return null;
};
5 changes: 5 additions & 0 deletions src/Map/Component/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import MapView from './MapView';
import Marker from './Marker';
import Polyline from './Polyline';

export { MapView, Marker, Polyline };
40 changes: 14 additions & 26 deletions src/Map/index.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
import type { ILayer, Scene } from '@antv/l7';
import { LarkMap } from '@antv/larkmap';
import React, { type FC } from 'react';
import React, { useMemo, type FC } from 'react';
import type { BaseMapProps } from '../types';
import { formatMapStyle, setMapContext, setMapView, setMarkers, setPolyline } from '../utils/map';
import { formatMapStyle } from '../utils/map';
import { MapView, Marker, Polyline } from './Component/';

export type MapProps = Omit<BaseMapProps<any>, 'data'>;

const Map: FC<MapProps> = (props) => {
const { className, containerStyle, children } = props;
const allLayers: ILayer[] = [];
const mapConfig = formatMapStyle(props);

const onSceneLoaded = async (scene: Scene) => {
// 初始地图视野
setMapView(props, scene);
// 初始化地图资源和状态
await setMapContext(props, scene);

// 添加线图层
if (props.polyline) {
const polylineLayer = setPolyline(props.polyline || []);
allLayers.push(...polylineLayer);
}

// 添加标记
if (props.markers) {
const markerLayer = setMarkers(props.markers || []);
allLayers.push(...markerLayer);
const mapConfig = useMemo(() => formatMapStyle(props), [props]);
const onSceneLoaded = async () => {
if (props.onInitComplete) {
props.onInitComplete();
}

allLayers.forEach((item) => {
scene.addLayer(item);
});
};

return (
Expand All @@ -41,6 +22,13 @@ const Map: FC<MapProps> = (props) => {
{...mapConfig}
onSceneLoaded={onSceneLoaded}
>
{/* 设置地图状态 */}
<MapView {...props} />
{/* 初始化图片,并加载 Marker */}
<Marker {...props} />
{/* 初始化线,并加载 Polyline */}
<Polyline {...props} />

{children}
</LarkMap>
);
Expand Down
3 changes: 3 additions & 0 deletions src/utils/map/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export function setMapContext(props: Map, scene: Scene) {
return Promise.all(
Array.from(icons.values()).map(async (url: string) => {
const id = urlToMarkerId(url);
if (scene.hasImage(id)) {
return;
}
return await scene.addImage(id, url);
}),
);
Expand Down
13 changes: 7 additions & 6 deletions src/utils/map/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ function fitIncludePoints(
}

export const setMapView = (props: Map, scene: Scene) => {
// 单个点,多个点
fitBounds(props, scene);
setMapStatus(props, scene);
};

export const fitBounds = (props: Map, scene: Scene) => {
if (props.includePoints) {
fitIncludePoints(props.includePoints, scene, props.includePadding);
} else {
Expand All @@ -53,7 +57,9 @@ export const setMapView = (props: Map, scene: Scene) => {
fitIncludePoints(points, scene, props.includePadding);
}
}
};

export const setMapStatus = (props: Map, scene: Scene) => {
if (props.enableZoom !== undefined) {
scene.setMapStatus({
zoomEnable: props.enableZoom,
Expand All @@ -69,9 +75,4 @@ export const setMapView = (props: Map, scene: Scene) => {
dragEnable: props.enableScroll,
});
}

if (props.onInitComplete) {
scene.off('loaded', props.onInitComplete);
scene.on('loaded', props.onInitComplete);
}
};
Loading