@hzab/map-combine 0.3.1-alpha.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,185 +1,185 @@
1
- import { AMap } from "./AMap";
2
- import { Polyline } from "../basic/Polyline";
3
-
4
- export function drawPolyline(map: AMap, polyline: Polyline<unknown>) {
5
- const { event, viewer, LbsAMap } = map;
6
-
7
- let status = 0;
8
- let skip = 0;
9
- let target = null;
10
- let aMapPolylineEditor = null;
11
-
12
- const _event = {
13
- cursor: polyline.cursor,
14
- silent: polyline.silent,
15
- onPointerIn() {
16
- status == 2 && polyline.event.trigger("pointer-in");
17
- },
18
- onPointerOut() {
19
- status == 2 && polyline.event.trigger("pointer-out");
20
- },
21
- onLClick() {
22
- status == 2 && polyline.event.trigger("left-click");
23
- },
24
- onRClick(e) {
25
- event.canvas = [e.pixel.x, e.pixel.y];
26
- if (polyline.menu) {
27
- map.menu.show(polyline.menu);
28
- }
29
- status == 2 && polyline.event.trigger("right-click");
30
- },
31
- onDbClick() {
32
- polyline.editable &&
33
- status == 2 &&
34
- setTimeout(() => {
35
- aMapPolylineEditor.setTarget(target);
36
- aMapPolylineEditor.open();
37
- polyline.event.trigger("start-edit");
38
- });
39
- },
40
- };
41
-
42
- const bindEvent = (graphical) => {
43
- graphical.on("click", _event.onLClick);
44
- graphical.on("rightclick", _event.onRClick);
45
- graphical.on("dblclick", _event.onDbClick);
46
- graphical.on("mouseover", _event.onPointerIn);
47
- graphical.on("mouseout", _event.onPointerOut);
48
- };
49
-
50
- aMapPolylineEditor = new LbsAMap.PolylineEditor(viewer, undefined, {
51
- createOptions: {
52
- strokeColor: polyline.stroke,
53
- strokeWeight: polyline.lineWidth,
54
- strokeStyle: polyline.dash ? "dashed" : "solid",
55
- strokeDasharray: [10, 10],
56
- },
57
- editOptions: {
58
- strokeColor: polyline.stroke,
59
- strokeWeight: polyline.lineWidth,
60
- strokeStyle: polyline.dash ? "dashed" : "solid",
61
- strokeDasharray: [10, 10],
62
- },
63
- });
64
-
65
- aMapPolylineEditor.on("add", (e) => {
66
- status = 3;
67
- target = e.target;
68
- bindEvent(target);
69
- // polygon.event.trigger("data-loaded");
70
- });
71
-
72
- if (polyline.coordinates) {
73
- status = 2;
74
- target = new LbsAMap.Polyline({
75
- path: polyline.coordinates,
76
- strokeColor: polyline.stroke,
77
- strokeWeight: polyline.lineWidth,
78
- strokeStyle: polyline.dash ? "dashed" : "solid",
79
- strokeDasharray: [10, 10],
80
- });
81
- viewer.add(target);
82
- bindEvent(target);
83
- } else {
84
- aMapPolylineEditor.close();
85
- aMapPolylineEditor.setTarget();
86
- aMapPolylineEditor.open();
87
- }
88
- const onUpdate = (e: Set<string>) => {
89
- if (skip) {
90
- skip--;
91
- return;
92
- }
93
-
94
- e.forEach((key) => {
95
- switch (key) {
96
- case "show":
97
- if (polyline.show) {
98
- target.show();
99
- } else {
100
- target.hide();
101
- }
102
- break;
103
- case "editable":
104
- break;
105
- case "coordinates":
106
- if (polyline.coordinates) {
107
- target.setPath(polyline.coordinates);
108
- target.setzIndex(polyline.coordinates[2]);
109
- target.show();
110
- status = 2;
111
- } else {
112
- target.hide();
113
- status = 0;
114
- }
115
- break;
116
-
117
- case "lineWidth":
118
- target.setOptions({
119
- strokeWeight: polyline.lineWidth,
120
- });
121
- break;
122
- case "stroke":
123
- target.setOptions({
124
- strokeColor: polyline.stroke,
125
- });
126
- break;
127
- case "dash":
128
- target.setOptions({
129
- strokeStyle: polyline.dash ? "dashed" : "solid",
130
- });
131
- break;
132
- default:
133
- throw new Error(`${key} 还不支持修改`);
134
- }
135
- });
136
- };
137
- const onDbClick = () => {
138
- switch (status) {
139
- case 1:
140
- status = 2;
141
- skip++;
142
- polyline.coordinates = target.getPath().map((item) => [item.lng, item.lat]);
143
- polyline.event.trigger("data-loaded");
144
- polyline.event.trigger("pointer-in");
145
- break;
146
- case 3:
147
- polyline.event.trigger("stop-edit");
148
- aMapPolylineEditor.setTarget();
149
- aMapPolylineEditor.close();
150
- break;
151
- }
152
- };
153
- const onStartEdit = () => {
154
- switch (status) {
155
- case 2:
156
- status = 3;
157
- break;
158
- }
159
- };
160
-
161
- const onStopEdit = () => {
162
- if (status == 3) {
163
- status = 2;
164
- skip++;
165
- polyline.coordinates = target.getPath().map((item) => [item.lng, item.lat]);
166
- }
167
- };
168
-
169
- const onDestroy = () => {
170
- viewer.remove(target);
171
- aMapPolylineEditor.close();
172
- aMapPolylineEditor.setTarget();
173
- event.off("double-click", onDbClick);
174
- polyline.event.off("update", onUpdate);
175
- polyline.event.off("start-edit", onStartEdit);
176
- polyline.event.off("stop-edit", onStopEdit);
177
- polyline.event.off("destroy", onDestroy);
178
- };
179
-
180
- event.on("double-click", onDbClick);
181
- polyline.event.on("update", onUpdate);
182
- polyline.event.on("start-edit", onStartEdit);
183
- polyline.event.on("stop-edit", onStopEdit);
184
- polyline.event.on("destroy", onDestroy);
185
- }
1
+ import { AMap } from "./AMap";
2
+ import { Polyline } from "../basic/Polyline";
3
+
4
+ export function drawPolyline(map: AMap, polyline: Polyline<unknown>) {
5
+ const { event, viewer, LbsAMap } = map;
6
+
7
+ let status = 0;
8
+ let skip = 0;
9
+ let target = null;
10
+ let aMapPolylineEditor = null;
11
+
12
+ const _event = {
13
+ cursor: polyline.cursor,
14
+ silent: polyline.silent,
15
+ onPointerIn() {
16
+ status == 2 && polyline.event.trigger("pointer-in");
17
+ },
18
+ onPointerOut() {
19
+ status == 2 && polyline.event.trigger("pointer-out");
20
+ },
21
+ onLClick() {
22
+ status == 2 && polyline.event.trigger("left-click");
23
+ },
24
+ onRClick(e) {
25
+ event.canvas = [e.pixel.x, e.pixel.y];
26
+ if (polyline.menu) {
27
+ map.menu.show(polyline.menu);
28
+ }
29
+ status == 2 && polyline.event.trigger("right-click");
30
+ },
31
+ onDbClick() {
32
+ polyline.editable &&
33
+ status == 2 &&
34
+ setTimeout(() => {
35
+ aMapPolylineEditor.setTarget(target);
36
+ aMapPolylineEditor.open();
37
+ polyline.event.trigger("start-edit");
38
+ });
39
+ },
40
+ };
41
+
42
+ const bindEvent = (graphical) => {
43
+ graphical.on("click", _event.onLClick);
44
+ graphical.on("rightclick", _event.onRClick);
45
+ graphical.on("dblclick", _event.onDbClick);
46
+ graphical.on("mouseover", _event.onPointerIn);
47
+ graphical.on("mouseout", _event.onPointerOut);
48
+ };
49
+
50
+ aMapPolylineEditor = new LbsAMap.PolylineEditor(viewer, undefined, {
51
+ createOptions: {
52
+ strokeColor: polyline.stroke,
53
+ strokeWeight: polyline.lineWidth,
54
+ strokeStyle: polyline.dash ? "dashed" : "solid",
55
+ strokeDasharray: [10, 10],
56
+ },
57
+ editOptions: {
58
+ strokeColor: polyline.stroke,
59
+ strokeWeight: polyline.lineWidth,
60
+ strokeStyle: polyline.dash ? "dashed" : "solid",
61
+ strokeDasharray: [10, 10],
62
+ },
63
+ });
64
+
65
+ aMapPolylineEditor.on("add", (e) => {
66
+ status = 3;
67
+ target = e.target;
68
+ bindEvent(target);
69
+ // polygon.event.trigger("data-loaded");
70
+ });
71
+
72
+ if (polyline.coordinates) {
73
+ status = 2;
74
+ target = new LbsAMap.Polyline({
75
+ path: polyline.coordinates,
76
+ strokeColor: polyline.stroke,
77
+ strokeWeight: polyline.lineWidth,
78
+ strokeStyle: polyline.dash ? "dashed" : "solid",
79
+ strokeDasharray: [10, 10],
80
+ });
81
+ viewer.add(target);
82
+ bindEvent(target);
83
+ } else {
84
+ aMapPolylineEditor.close();
85
+ aMapPolylineEditor.setTarget();
86
+ aMapPolylineEditor.open();
87
+ }
88
+ const onUpdate = (e: Set<string>) => {
89
+ if (skip) {
90
+ skip--;
91
+ return;
92
+ }
93
+
94
+ e.forEach((key) => {
95
+ switch (key) {
96
+ case "show":
97
+ if (polyline.show) {
98
+ target.show();
99
+ } else {
100
+ target.hide();
101
+ }
102
+ break;
103
+ case "editable":
104
+ break;
105
+ case "coordinates":
106
+ if (polyline.coordinates) {
107
+ target.setPath(polyline.coordinates);
108
+ target.setzIndex(polyline.coordinates[2]);
109
+ target.show();
110
+ status = 2;
111
+ } else {
112
+ target.hide();
113
+ status = 0;
114
+ }
115
+ break;
116
+
117
+ case "lineWidth":
118
+ target.setOptions({
119
+ strokeWeight: polyline.lineWidth,
120
+ });
121
+ break;
122
+ case "stroke":
123
+ target.setOptions({
124
+ strokeColor: polyline.stroke,
125
+ });
126
+ break;
127
+ case "dash":
128
+ target.setOptions({
129
+ strokeStyle: polyline.dash ? "dashed" : "solid",
130
+ });
131
+ break;
132
+ default:
133
+ throw new Error(`${key} 还不支持修改`);
134
+ }
135
+ });
136
+ };
137
+ const onDbClick = () => {
138
+ switch (status) {
139
+ case 1:
140
+ status = 2;
141
+ skip++;
142
+ polyline.coordinates = target.getPath().map((item) => [item.lng, item.lat]);
143
+ polyline.event.trigger("data-loaded");
144
+ polyline.event.trigger("pointer-in");
145
+ break;
146
+ case 3:
147
+ polyline.event.trigger("stop-edit");
148
+ aMapPolylineEditor.setTarget();
149
+ aMapPolylineEditor.close();
150
+ break;
151
+ }
152
+ };
153
+ const onStartEdit = () => {
154
+ switch (status) {
155
+ case 2:
156
+ status = 3;
157
+ break;
158
+ }
159
+ };
160
+
161
+ const onStopEdit = () => {
162
+ if (status == 3) {
163
+ status = 2;
164
+ skip++;
165
+ polyline.coordinates = target.getPath().map((item) => [item.lng, item.lat]);
166
+ }
167
+ };
168
+
169
+ const onDestroy = () => {
170
+ viewer.remove(target);
171
+ aMapPolylineEditor.close();
172
+ aMapPolylineEditor.setTarget();
173
+ event.off("double-click", onDbClick);
174
+ polyline.event.off("update", onUpdate);
175
+ polyline.event.off("start-edit", onStartEdit);
176
+ polyline.event.off("stop-edit", onStopEdit);
177
+ polyline.event.off("destroy", onDestroy);
178
+ };
179
+
180
+ event.on("double-click", onDbClick);
181
+ polyline.event.on("update", onUpdate);
182
+ polyline.event.on("start-edit", onStartEdit);
183
+ polyline.event.on("stop-edit", onStopEdit);
184
+ polyline.event.on("destroy", onDestroy);
185
+ }
@@ -1,94 +1,98 @@
1
- import { ContextMenu } from "./ContextMenu";
2
- import { MapElement } from "./MapElement";
3
- import { MapEvent } from "./MapEvent";
4
- import { Point, PointOption } from "./Point";
5
- import { Polygon, PolygonOption } from "./Polygon";
6
- import { Polyline, PolylineOption } from "./Polyline";
7
- import { ReactPoint, ReactPointOption, drawReactPoint } from "./ReactPoint";
8
- import { Tile3D, Tile3DOption } from "./Tile3D";
9
- import { type BasicBusiness, type BasicBusinessOption } from "./BasicBusiness";
10
-
11
- import { getBounds } from "../utils/points-viewer";
12
-
13
- export abstract class MapCombine {
14
- /* 用于挂载html元素的根节点 */
15
- readonly htmllayer = document.createElement("div");
16
- readonly elements = new Map<string, MapElement>();
17
- readonly event = new MapEvent();
18
- readonly menu: ContextMenu;
19
- abstract get cursor(): string;
20
- abstract set cursor(val: string);
21
-
22
- abstract get moveable(): boolean;
23
- abstract set moveable(val: boolean);
24
-
25
- abstract get center(): number[];
26
- abstract set center(val);
27
-
28
- abstract get zoom(): number;
29
- abstract set zoom(val);
30
-
31
- loadPromise: Promise<this> = Promise.resolve(this);
32
-
33
- bussinesses = new Map<string, BasicBusiness<BasicBusinessOption>>();
34
-
35
- /** 地图是否存在 */
36
- isAlive = true;
37
- constructor() {
38
- const { htmllayer } = this;
39
- htmllayer.style.zIndex = "1000";
40
- htmllayer.style.position = "absolute";
41
- htmllayer.style.width = "0";
42
- htmllayer.style.height = "0";
43
- htmllayer.style.left = "0";
44
- htmllayer.style.top = "0";
45
- this.menu = new ContextMenu(this);
46
- (window as any).map = this;
47
- setTimeout(() => {
48
- this.bussinesses.forEach((e) => e.show && e.onMapLoaded?.());
49
- }, 100);
50
- }
51
-
52
- createReactPoint<K>(option?: Partial<ReactPointOption<K>>): ReactPoint<K> {
53
- const e = new ReactPoint(this, option);
54
- drawReactPoint(this, e);
55
- return e;
56
- }
57
-
58
- /**
59
- * 设置地图视图
60
- * @param option 视图参数
61
- * @param time 设置时间会动画形式调整视图
62
- */
63
- abstract createPoint<K>(option?: Partial<PointOption<K>>): Point<K>;
64
-
65
- abstract createPolyline<K>(option?: Partial<PolylineOption<K>>): Polyline<K>;
66
-
67
- abstract createPolygon<K>(option?: Partial<PolygonOption<K>>): Polygon<K>;
68
-
69
- abstract createTile3D(option?: Partial<Tile3DOption>): Tile3D;
70
-
71
- abstract canvasTgeography(p: number[]): number[];
72
-
73
- abstract geographyTcanvas(p: number[]): number[];
74
-
75
- /**
76
- * 所有点位移动至视口,调整位置及层级
77
- * @param points
78
- */
79
- abstract flyToViewer(points: number[][], opt?: Object): void;
80
-
81
- /**
82
- * 移动点位到中心点
83
- * @param points
84
- */
85
- abstract flyTo(point: number[], opt?: Object): void;
86
-
87
- getBounds = getBounds;
88
-
89
- destroy() {
90
- this.isAlive = false;
91
- this.htmllayer.remove();
92
- this.event.trigger("destroy");
93
- }
94
- }
1
+ import { ContextMenu } from "./ContextMenu";
2
+ import { MapElement } from "./MapElement";
3
+ import { MapEvent } from "./MapEvent";
4
+ import { Point, PointOption } from "./Point";
5
+ import { PointAggregation, PointAggregationOption } from "./PointAggregation";
6
+ import { Polygon, PolygonOption } from "./Polygon";
7
+ import { Polyline, PolylineOption } from "./Polyline";
8
+ import { ReactPoint, ReactPointOption, drawReactPoint } from "./ReactPoint";
9
+ import { Tile3D, Tile3DOption } from "./Tile3D";
10
+ import { type BasicBusiness, type BasicBusinessOption } from "./BasicBusiness";
11
+
12
+ import { getBounds } from "../utils/points-viewer";
13
+
14
+ export abstract class MapCombine {
15
+ /* 用于挂载html元素的根节点 */
16
+ readonly htmllayer = document.createElement("div");
17
+ readonly elements = new Map<string, MapElement>();
18
+ readonly event = new MapEvent();
19
+ readonly menu: ContextMenu;
20
+ abstract get cursor(): string;
21
+ abstract set cursor(val: string);
22
+
23
+ abstract get moveable(): boolean;
24
+ abstract set moveable(val: boolean);
25
+
26
+ abstract get center(): number[];
27
+ abstract set center(val);
28
+
29
+ abstract get zoom(): number;
30
+ abstract set zoom(val);
31
+
32
+ loadPromise: Promise<this> = Promise.resolve(this);
33
+
34
+ bussinesses = new Map<string, BasicBusiness<BasicBusinessOption>>();
35
+
36
+ /** 地图是否存在 */
37
+ isAlive = true;
38
+ constructor() {
39
+ const { htmllayer } = this;
40
+ htmllayer.style.zIndex = "1000";
41
+ htmllayer.style.position = "absolute";
42
+ htmllayer.style.width = "0";
43
+ htmllayer.style.height = "0";
44
+ htmllayer.style.left = "0";
45
+ htmllayer.style.top = "0";
46
+ this.menu = new ContextMenu(this);
47
+ (window as any).map = this;
48
+ setTimeout(() => {
49
+ this.bussinesses.forEach((e) => e.show && e.onMapLoaded?.());
50
+ }, 100);
51
+ }
52
+
53
+ createReactPoint<K>(option?: Partial<ReactPointOption<K>>): ReactPoint<K> {
54
+ const e = new ReactPoint(this, option);
55
+ drawReactPoint(this, e);
56
+ return e;
57
+ }
58
+
59
+ /**
60
+ * 设置地图视图
61
+ * @param option 视图参数
62
+ * @param time 设置时间会动画形式调整视图
63
+ */
64
+ abstract createPoint<K>(option?: Partial<PointOption<K>>): Point<K>;
65
+
66
+ /** 聚合点 */
67
+ abstract createPointAggregation(option?: PointAggregationOption): PointAggregation;
68
+
69
+ abstract createPolyline<K>(option?: Partial<PolylineOption<K>>): Polyline<K>;
70
+
71
+ abstract createPolygon<K>(option?: Partial<PolygonOption<K>>): Polygon<K>;
72
+
73
+ abstract createTile3D(option?: Partial<Tile3DOption>): Tile3D;
74
+
75
+ abstract canvasTgeography(p: number[]): number[];
76
+
77
+ abstract geographyTcanvas(p: number[]): number[];
78
+
79
+ /**
80
+ * 所有点位移动至视口,调整位置及层级
81
+ * @param points
82
+ */
83
+ abstract flyToViewer(points: number[][], opt?: Object): void;
84
+
85
+ /**
86
+ * 移动点位到中心点
87
+ * @param points
88
+ */
89
+ abstract flyTo(point: number[], opt?: Object): void;
90
+
91
+ getBounds = getBounds;
92
+
93
+ destroy() {
94
+ this.isAlive = false;
95
+ this.htmllayer.remove();
96
+ this.event.trigger("destroy");
97
+ }
98
+ }
@@ -1,5 +1,5 @@
1
1
  import { ReactElement } from "react";
2
- import { throttle } from 'lodash';
2
+ import { throttle } from "lodash";
3
3
 
4
4
  import { LATTOONE, LONTOONE } from "../utils/static";
5
5
  import { getUUID, setValue } from "../utils";
@@ -11,13 +11,16 @@ export interface PointAggregationOption {
11
11
  name: string;
12
12
  /** 组件是否显示 */
13
13
  show: boolean;
14
+ /** 数据点 */
14
15
  datas: any[];
15
16
  /** 小于该值的等级下会聚合 */
16
17
  maxZoomLevel?: number;
17
18
  /** 值越大聚合点的间距越小 */
18
19
  density?: number;
19
- element(e: any[], p: number[]): ReactElement;
20
- location(e: any): number[];
20
+ /** 点的展示元素 */
21
+ element(datas: any[], p: number[]): ReactElement;
22
+ /** 坐标处理回调 */
23
+ location(lonlat: number[]): number[];
21
24
  }
22
25
 
23
26
  class TileNode {
@@ -41,7 +44,7 @@ class TileNode {
41
44
  const option = {
42
45
  show: true,
43
46
  name: getUUID(),
44
- element: () => (this._element),
47
+ element: () => this._element,
45
48
  center: [0.5, 0.5],
46
49
  coordinates: [this._location[0], this._location[1], 0],
47
50
  editable: false,
@@ -53,17 +56,18 @@ class TileNode {
53
56
  } else {
54
57
  if (this.marker) {
55
58
  this.marker.destroy();
59
+ this.marker = undefined;
56
60
  }
57
61
  }
58
62
  }
59
63
  }
60
64
 
61
-
62
65
  export class PointAggregation {
63
66
  private readonly _cache = new Map<number, TileNode[]>();
64
67
  private _current = 0;
65
68
  private _map: MapCombine;
66
69
  private _option: PointAggregationOption;
70
+ private _onAfterViewChange;
67
71
 
68
72
  /** 唯一标识 */
69
73
  get name() {
@@ -86,10 +90,10 @@ export class PointAggregation {
86
90
  this._map = map;
87
91
  this._option = option;
88
92
  const { event } = map;
93
+ // _onAfterViewChange 解决元素销毁 事件取消监听异常问题
94
+ this._onAfterViewChange = this.onAfterViewChange.bind(this);
89
95
  this.onAfterViewChange();
90
- event.on("view-moveEnd", () => {
91
- this.onAfterViewChange()
92
- });
96
+ event.on("view-moveEnd", this._onAfterViewChange);
93
97
  }
94
98
 
95
99
  private _turnon(zoom: number) {
@@ -102,7 +106,7 @@ export class PointAggregation {
102
106
  const grid = new Map();
103
107
  const max = Math.pow(2, zoom) * (_option.density ?? 1);
104
108
  _option.datas.forEach((data) => {
105
- const location = _option.location(data);
109
+ const location = typeof _option.location === "function" ? _option.location(data) : data;
106
110
  const x = Math.floor(LONTOONE.forward(location[0]) * max * 2);
107
111
  const y = Math.floor(LATTOONE.forward(location[1]) * max);
108
112
  const k = `${x}_${y}`;
@@ -113,10 +117,11 @@ export class PointAggregation {
113
117
  grid.set(k, [data]);
114
118
  }
115
119
  });
120
+ // 处理聚合点坐标
116
121
  grid.forEach((e) => {
117
122
  const { lon, lat } = e.reduce(
118
123
  (a, b) => {
119
- const loaction = _option.location(b);
124
+ const loaction = typeof _option.location === "function" ? _option.location(b) : b;
120
125
  a.lon += loaction[0];
121
126
  a.lat += loaction[1];
122
127
  return a;
@@ -178,7 +183,7 @@ export class PointAggregation {
178
183
  /** 销毁组件 */
179
184
  destroy() {
180
185
  const { event } = this._map;
181
- event.on("view-moveEnd", null);
186
+ event.off("view-moveEnd", this._onAfterViewChange);
182
187
  this._map.elements.delete(this.name);
183
188
  this._cache.forEach((e) => {
184
189
  e.forEach((f) => {