|
|
|
@ -16,7 +16,6 @@ import { IMapOptions, IMapEditor } from "./type";
|
|
|
|
|
import { PolygonOptions, PolylineOptions, SelectedOptions } from "./constants"; |
|
|
|
|
|
|
|
|
|
import { EventTypes, OverlayTypes } from "../types/enum"; |
|
|
|
|
import { Position } from "geojson"; |
|
|
|
|
|
|
|
|
|
type AMapOverlayEditor = |
|
|
|
|
| AMap.RectangleEditor |
|
|
|
@ -95,16 +94,22 @@ export class MapEditor extends Emitter implements IMapEditor {
|
|
|
|
|
getEditorByType(overlayType: OverlayTypes) { |
|
|
|
|
return this.overlayEditors.find( |
|
|
|
|
(editor) => editor.getType() === overlayType |
|
|
|
|
); |
|
|
|
|
)!; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
importProject(options: IMapOptions) { |
|
|
|
|
this.clearOverlays(); |
|
|
|
|
const { rectangles, polygons, polylines, circles } = options; |
|
|
|
|
const rectangleEditor = this.getEditorByType(OverlayTypes.Rectangle); |
|
|
|
|
const polygonEditor = this.getEditorByType(OverlayTypes.Polygon); |
|
|
|
|
const polylineEditor = this.getEditorByType(OverlayTypes.Polyline); |
|
|
|
|
const circleEditor = this.getEditorByType(OverlayTypes.Circle); |
|
|
|
|
const { overlays } = options; |
|
|
|
|
|
|
|
|
|
const overlayEditors: Record< |
|
|
|
|
OverlayTypes, |
|
|
|
|
BaseOverlayEditor<AMapOverlayEditor> |
|
|
|
|
> = { |
|
|
|
|
[OverlayTypes.Rectangle]: this.getEditorByType(OverlayTypes.Rectangle), |
|
|
|
|
[OverlayTypes.Polygon]: this.getEditorByType(OverlayTypes.Polygon), |
|
|
|
|
[OverlayTypes.Polyline]: this.getEditorByType(OverlayTypes.Polyline), |
|
|
|
|
[OverlayTypes.Circle]: this.getEditorByType(OverlayTypes.Circle), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if (options.center) { |
|
|
|
|
const [lng, lat] = options.center; |
|
|
|
@ -114,11 +119,9 @@ export class MapEditor extends Emitter implements IMapEditor {
|
|
|
|
|
this.map.setZoom(options.zoom); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const addOverlay = ( |
|
|
|
|
id: string, |
|
|
|
|
target: AMap.MapOverlay, |
|
|
|
|
type: OverlayTypes |
|
|
|
|
) => { |
|
|
|
|
overlays.forEach((overlay) => { |
|
|
|
|
const { type, id } = overlay; |
|
|
|
|
const target = overlayEditors[type].build(overlay); |
|
|
|
|
this.overlayMap[id] = { |
|
|
|
|
type, |
|
|
|
|
target, |
|
|
|
@ -127,28 +130,7 @@ export class MapEditor extends Emitter implements IMapEditor {
|
|
|
|
|
type === OverlayTypes.Polyline ? PolylineOptions : PolygonOptions |
|
|
|
|
); |
|
|
|
|
this.map.add(target); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
rectangles.forEach((rect) => |
|
|
|
|
addOverlay(rect.id, rectangleEditor!.build(rect), OverlayTypes.Rectangle) |
|
|
|
|
); |
|
|
|
|
polygons.forEach((polygon) => |
|
|
|
|
addOverlay( |
|
|
|
|
polygon.id, |
|
|
|
|
polygonEditor!.build(polygon), |
|
|
|
|
OverlayTypes.Polygon |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
polylines.forEach((polyline) => |
|
|
|
|
addOverlay( |
|
|
|
|
polyline.id, |
|
|
|
|
polylineEditor!.build(polyline), |
|
|
|
|
OverlayTypes.Polyline |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
circles.forEach((circle) => |
|
|
|
|
addOverlay(circle.id, circleEditor!.build(circle), OverlayTypes.Circle) |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
importGeoJSON(geojson: GeoJSON.FeatureCollection): void { |
|
|
|
|