From c136212d8ad2e8fa30f057151b6e64a96ab4e601 Mon Sep 17 00:00:00 2001 From: Cmen <1176967590@qq.com> Date: Sat, 26 Feb 2022 09:53:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=98=E5=82=A8path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/editor/Catalog/index.tsx | 4 +--- src/index.tsx | 3 ++- src/map/index.ts | 6 ++++-- src/store/reducers/map/index.ts | 1 - src/store/type.ts | 2 ++ src/utils/index.ts | 29 +++++++++++++++++++++++++++++ 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/editor/Catalog/index.tsx b/src/editor/Catalog/index.tsx index c9675e7..d074312 100644 --- a/src/editor/Catalog/index.tsx +++ b/src/editor/Catalog/index.tsx @@ -29,13 +29,11 @@ const OverlayList = (props: OverlayListProps) => { }; const Catalog = () => { - // const callback = (key: string | string[]) => console.log(key); const { rectangles, polygons, polylines, circles } = useSelector(mapStateSelector); return ( diff --git a/src/index.tsx b/src/index.tsx index adc8356..f9e6062 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,8 +4,9 @@ import { Provider } from "react-redux"; import "antd/dist/antd.css"; -import { store, StoreContext } from "./store"; +import { store } from "./store"; import { Editor } from "./editor"; + import "./index.less"; ReactDOM.render( diff --git a/src/map/index.ts b/src/map/index.ts index 943d476..f848e23 100644 --- a/src/map/index.ts +++ b/src/map/index.ts @@ -12,6 +12,7 @@ import { import { registerHotkey } from "../utils/hotkeys"; import { EventTypes } from "../types/enum"; +import { getOverlayPaths } from "@utils"; let uuid = 0; const getUuid = () => ++uuid; @@ -72,14 +73,15 @@ export class MapEditor extends Emitter { finishCreateOverlay() { if (this._currentOverlayEditor == null) return; const target = this._currentOverlayEditor.finish(); - console.log(target); let evt: any = null; if (target != null) { const id = getUuid(); + const type = this._currentOverlayEditor.getType(); this.overlayMap[id] = target; evt = { id, - type: this._currentOverlayEditor.getType(), + type, + path: getOverlayPaths(target, type), }; } evt && this.emit(EventTypes.FinishCreateOverlay, evt); diff --git a/src/store/reducers/map/index.ts b/src/store/reducers/map/index.ts index f1352a3..c7d511c 100644 --- a/src/store/reducers/map/index.ts +++ b/src/store/reducers/map/index.ts @@ -26,6 +26,5 @@ export function finishCreateOverlay(state = initState, payload: any) { ? draft.map.polygons : draft.map.polylines ).push(overlay); - // draft.map.rectangles = state.map.rectangles.concat([overlay]); }); } diff --git a/src/store/type.ts b/src/store/type.ts index 2f271c2..ce3d5d8 100644 --- a/src/store/type.ts +++ b/src/store/type.ts @@ -4,6 +4,8 @@ export interface IOverlay { id: string; name: string; type: OverlayTypes; + lngLat?: number[]; + paths?: number[][]; } export interface IMapState { diff --git a/src/utils/index.ts b/src/utils/index.ts index 68e29d3..912d418 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1 +1,30 @@ +import { OverlayTypes } from "@types"; + export * from "./hotkeys"; + +export function getOverlayPaths(target: AMap.MapOverlay, type: OverlayTypes) { + if (type === OverlayTypes.Rectangle) { + const overlay = target as AMap.Rectangle; + const bounds = overlay.getBounds(); + const [west, south, east, north] = getBBox(bounds!); + return [ + [west, south], + [west, north], + [east, north], + [east, south], + ]; + } + + const overlay = target as AMap.Polygon | AMap.Polyline; + return overlay.getPath()?.map((path) => { + const lngLat = path as AMap.LngLat; + return [lngLat.lng, lngLat.lat] || []; + }); +} + +function getBBox(bounds: AMap.Bounds) { + const { northEast, southWest } = bounds; + let { lng: east, lat: north } = northEast; + let { lng: west, lat: south } = southWest; + return [west, south, east, north]; +}