Browse Source

保存地图中心点&缩放层级

master
Cmen 3 years ago
parent
commit
e8f872c234
  1. 17
      src/editor/Plot/Tools/index.tsx
  2. 18
      src/map/MapEditor.ts
  3. 4
      src/map/type.ts
  4. 6
      src/store/actions/index.ts

17
src/editor/Plot/Tools/index.tsx

@ -38,11 +38,12 @@ const IconWithTip = ({
type OverlayToolProps = { type OverlayToolProps = {
type: OverlayTypes; type: OverlayTypes;
hotkey: string;
}; };
const OverlayTool = (props: OverlayToolProps) => { const OverlayTool = (props: OverlayToolProps) => {
const { type } = props; const { type, hotkey } = props;
const text = OverlayNamePrefixs[type]; const text = `创建${OverlayNamePrefixs[type]}(${hotkey})`;
const overlayType = useSelector(overlayTypeSelector); const overlayType = useSelector(overlayTypeSelector);
const status = useSelector(statusSelector); const status = useSelector(statusSelector);
@ -57,18 +58,18 @@ const Tools = () => {
return ( return (
<> <>
<div className="marker-tools"> <div className="marker-tools">
<OverlayTool type={OverlayTypes.Rectangle} /> <OverlayTool type={OverlayTypes.Rectangle} hotkey="Alt+1" />
<OverlayTool type={OverlayTypes.Polygon} /> <OverlayTool type={OverlayTypes.Polygon} hotkey="Alt+2" />
<OverlayTool type={OverlayTypes.Polyline} /> <OverlayTool type={OverlayTypes.Polyline} hotkey="Alt+3" />
<OverlayTool type={OverlayTypes.Circle} /> <OverlayTool type={OverlayTypes.Circle} hotkey="Alt+4" />
</div> </div>
<div className="common-tools"> <div className="common-tools">
<div className="tools-group"> <div className="tools-group">
<IconWithTip type="search" text="搜索定位" placement="right" /> <IconWithTip type="search" text="搜索定位" placement="right" />
</div> </div>
<div className="tools-group"> {/* <div className="tools-group">
<IconWithTip type="select" text="选择" placement="right" /> <IconWithTip type="select" text="选择" placement="right" />
</div> </div> */}
</div> </div>
</> </>
); );

18
src/map/MapEditor.ts

@ -16,6 +16,7 @@ import { IMapOptions, IMapEditor } from "./type";
import { PolygonOptions, PolylineOptions, SelectedOptions } from "./constants"; import { PolygonOptions, PolylineOptions, SelectedOptions } from "./constants";
import { EventTypes, OverlayTypes } from "../types/enum"; import { EventTypes, OverlayTypes } from "../types/enum";
import { Position } from "geojson";
type AMapOverlayEditor = type AMapOverlayEditor =
| AMap.RectangleEditor | AMap.RectangleEditor
@ -82,6 +83,15 @@ export class MapEditor extends Emitter implements IMapEditor {
]; ];
} }
getCenter() {
const { lng, lat } = this.map.getCenter();
return [lng, lat];
}
getZoom() {
return this.map.getZoom();
}
getEditorByType(overlayType: OverlayTypes) { getEditorByType(overlayType: OverlayTypes) {
return this.overlayEditors.find( return this.overlayEditors.find(
(editor) => editor.getType() === overlayType (editor) => editor.getType() === overlayType
@ -96,6 +106,14 @@ export class MapEditor extends Emitter implements IMapEditor {
const polylineEditor = this.getEditorByType(OverlayTypes.Polyline); const polylineEditor = this.getEditorByType(OverlayTypes.Polyline);
const circleEditor = this.getEditorByType(OverlayTypes.Circle); const circleEditor = this.getEditorByType(OverlayTypes.Circle);
if (options.center) {
const [lng, lat] = options.center;
this.map.setCenter(new AMap.LngLat(lng, lat));
}
if (options.zoom != null) {
this.map.setZoom(options.zoom);
}
const addOverlay = ( const addOverlay = (
id: string, id: string,
target: AMap.MapOverlay, target: AMap.MapOverlay,

4
src/map/type.ts

@ -9,6 +9,8 @@ export interface IMapEditor {
selectOverlays(ids?: string[]): void; selectOverlays(ids?: string[]): void;
deleteOverlays(): void; deleteOverlays(): void;
clearOverlays(): void; clearOverlays(): void;
getCenter(): GeoJSON.Position;
getZoom(): number;
} }
export interface IOverlay { export interface IOverlay {
@ -25,4 +27,6 @@ export interface IMapOptions {
circles: IOverlay[]; circles: IOverlay[];
rectangles: IOverlay[]; rectangles: IOverlay[];
selectedIds?: string[]; selectedIds?: string[];
center?: GeoJSON.Position;
zoom?: number;
} }

6
src/store/actions/index.ts

@ -49,8 +49,10 @@ export class EditorAction {
} }
setInterval(() => { setInterval(() => {
sessionStorage.setItem("fine-geojson", JSON.stringify(this.mapOptions)); const { mapOptions } = this;
console.log("updated!"); mapOptions.center = this.mapEditor?.getCenter();
mapOptions.zoom = this.mapEditor?.getZoom();
sessionStorage.setItem("fine-geojson", JSON.stringify(mapOptions));
}, 10000); }, 10000);
} }
// ---------- public methods ------------- // ---------- public methods -------------

Loading…
Cancel
Save