基于高德地图JS api开发的geojson编辑器. http://geojson.finevis.cc/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.1 KiB

import Emitter from "@finevis/emitter";
import "@amap/amap-jsapi-types";
import { IMapState } from "@store";
import { getOverlayPaths } from "@utils";
import {
BaseOverlayEditor,
RectangleEditor,
PolygonEditor,
PolylineEditor,
CircleEditor,
} from "./editors";
3 years ago
import { registerHotkey } from "../utils/hotkeys";
3 years ago
import { PolygonOptions, PolylineOptions, SelectedOptions } from "./constants";
3 years ago
import { Command, EventTypes, OverlayTypes } from "../types/enum";
let uuid = 0;
3 years ago
const getUuid = () => ++uuid + "";
type AMapOverlayEditor =
| AMap.RectangleEditor
| AMap.PolygonEditor
| AMap.PolylineEditor
| AMap.CircleEditor;
3 years ago
type OverlayTemp = {
type: OverlayTypes;
target: AMap.MapOverlay;
};
export class MapEditor extends Emitter {
3 years ago
dom: HTMLDivElement;
private _map: AMap.Map | undefined;
private overlayEditors: BaseOverlayEditor<AMapOverlayEditor>[] = [];
3 years ago
private overlayMap: Record<string, OverlayTemp> = {};
private _currentOverlayEditor:
| BaseOverlayEditor<AMapOverlayEditor>
| undefined;
3 years ago
private _selectedIds: string[] = [];
private _editorStatus: "editing" | "creating" | null = null;
3 years ago
constructor(dom: HTMLDivElement) {
super();
3 years ago
this.dom = dom;
}
get map() {
return this._map!;
3 years ago
}
async init() {
await AMapLoader.load({
key: "a4171ad2d7df42823b4de7d25c8c35ee",
version: "2.0",
plugins: [
"AMap.RectangleEditor",
"AMap.PolylineEditor",
"AMap.PolygonEditor",
"AMap.CircleEditor",
3 years ago
"AMap.PlaceSearch",
"AMap.AutoComplete",
],
});
this._map = new AMap.Map(this.dom);
this.initEditors();
// Space的key是空字符串, 这就离谱.
3 years ago
registerHotkey(" ", { callback: this._finishEditOverlay.bind(this) });
registerHotkey("e", {
callback: this._editSelectedOverlay.bind(this),
alt: true,
});
}
update(mapState: IMapState) {
3 years ago
if (this._editorStatus != null) {
this._finishEditOverlay();
}
3 years ago
const { command } = mapState;
switch (command) {
case Command.CreateOverlay:
this._createOverlay(mapState);
break;
case Command.SelectOverlay:
this._selectOverlays(mapState.selectedIds);
}
}
3 years ago
initEditors() {
const { map } = this;
this.overlayEditors = [
new RectangleEditor(map),
new PolygonEditor(map),
new PolylineEditor(map),
new CircleEditor(map),
];
}
getEditorByType(overlayType: OverlayTypes) {
return this.overlayEditors.find(
3 years ago
(editor) => editor.getType() === overlayType
);
3 years ago
}
_createOverlay(mapState: IMapState) {
const { overlayType } = mapState;
this._currentOverlayEditor = this.getEditorByType(overlayType!);
3 years ago
this._currentOverlayEditor?.create();
3 years ago
this._editorStatus = "creating";
3 years ago
}
_selectOverlays(ids?: string[]) {
if (!ids?.length) return;
3 years ago
this._selectedIds = ids;
3 years ago
ids.forEach((id) => {
const { target } = this.overlayMap[id];
target.setOptions(SelectedOptions);
});
}
3 years ago
_finishEditOverlay() {
if (this._currentOverlayEditor == null) return;
const target = this._currentOverlayEditor.finish();
if (target == null) return;
const type = this._currentOverlayEditor.getType();
3 years ago
const isCreatingOverlay = this._editorStatus === "creating";
let id = getUuid();
if (isCreatingOverlay) {
this.overlayMap[id] = { type, target };
} else {
id = this._selectedIds[0];
}
const evt: any = { id, type };
if (type === OverlayTypes.Circle) {
const circle = target as AMap.Circle;
evt.lngLat = circle.getCenter();
evt.radius = circle.getRadius();
} else {
evt.path = getOverlayPaths(target, type);
}
target.setOptions(
type === OverlayTypes.Polyline ? PolylineOptions : PolygonOptions
);
3 years ago
this.emit(EventTypes.FinishEditOverlay, evt);
this._editorStatus = null;
}
3 years ago
_editSelectedOverlay() {
if (this._selectedIds.length !== 1) return;
const [id] = this._selectedIds;
const { target, type } = this.overlayMap[id];
this._currentOverlayEditor = this.getEditorByType(type);
this._currentOverlayEditor?.edit(target);
this._editorStatus = "editing";
3 years ago
}
}