hn-map 1.1.2 → 1.1.3

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.
package/src/index.ts CHANGED
@@ -19,150 +19,154 @@ import geoJson from "./layer/geoJson";
19
19
  * HnMap类是一个地图工具类,用于创建和管理地图对象及其相关元素
20
20
  */
21
21
  interface option {
22
- lng: number;
23
- lat: number;
24
- level: number;
25
- pitch: number;
26
- gaode_key: string;
27
- sj_app_key: string;
28
- sj_app_secret: string;
29
- sj_js_url: string;
22
+ lng: number;
23
+ lat: number;
24
+ level: number;
25
+ pitch: number;
26
+ gaode_key: string;
27
+ sj_app_key: string;
28
+ sj_app_secret: string;
29
+ sj_js_url: string;
30
+ sj_style: string;
30
31
  }
31
32
 
32
33
  export default class HnMap {
33
- id: any = null;
34
- option: any = null;
35
- mapType: any = null;
36
- map: any = null;
37
- Layer: any = null;
38
- Point: any = null;
39
- NumPoint: any = null;
40
- ImagePoint: any = null;
41
- DivPoint: any = null;
42
- Label: any = null;
43
- Line: any = null;
44
- Circle: any = null;
45
- Rectangle: any = null;
46
- Polygon: any = null;
47
- Route: any = null;
48
- HeatMap: any = null;
49
- Cluster: any = null;
50
- PointCloud: any = null;
51
- GeoJson: any = null;
34
+ id: any = null;
35
+ option: any = null;
36
+ mapType: any = null;
37
+ map: any = null;
38
+ Layer: any = null;
39
+ Point: any = null;
40
+ NumPoint: any = null;
41
+ ImagePoint: any = null;
42
+ DivPoint: any = null;
43
+ Label: any = null;
44
+ Line: any = null;
45
+ Circle: any = null;
46
+ Rectangle: any = null;
47
+ Polygon: any = null;
48
+ Route: any = null;
49
+ HeatMap: any = null;
50
+ Cluster: any = null;
51
+ PointCloud: any = null;
52
52
 
53
- // 允许构造函数
54
- private static allowConstruction = false;
53
+ GeoJson: any = null;
55
54
 
56
- /**
57
- * HnMap类的构造函数
58
- * @param id 地图容器的ID
59
- * @param option 地图的配置选项
60
- * @param mapType 地图的类型
61
- * @param map
62
- */
63
- private constructor(id: string, option: option, mapType: string, map: any) {
64
- if (!HnMap.allowConstruction) {
65
- throw new Error("请使用 HnMap.create() 创建实例");
66
- }
67
- this.id = id;
68
- this.option = option;
69
- this.mapType = mapType;
70
- this.map = map; // ✅ 此时 map 已创建
55
+ // 允许构造函数
56
+ private static allowConstruction = false;
57
+
58
+ /**
59
+ * HnMap类的构造函数
60
+ * @param id 地图容器的ID
61
+ * @param option 地图的配置选项
62
+ * @param mapType 地图的类型
63
+ * @param map
64
+ */
65
+ private constructor(id: string, option: option, mapType: string, map: any) {
66
+ if (!HnMap.allowConstruction) {
67
+ throw new Error("请使用 HnMap.create() 创建实例");
71
68
  }
69
+ this.id = id;
70
+ this.option = option;
71
+ this.mapType = mapType;
72
+ this.map = map; // ✅ 此时 map 已创建
73
+ }
72
74
 
73
- /**
74
- * 创建HnMap实例的方法
75
- * @param id 地图容器的ID
76
- * @param option 地图的配置选项
77
- * @param mapType 地图的类型
78
- */
79
- static async create(id: string, option: option, mapType: string) {
80
- // 判断是否在子路径下
81
- const basePath = window.location.pathname.endsWith("/")
82
- ? window.location.pathname
83
- : window.location.pathname.substring(
84
- 0,
85
- window.location.pathname.lastIndexOf("/") + 1
86
- );
87
- await loadResource(basePath + "lib/turf/turf.min.js", "js");
75
+ /**
76
+ * 创建HnMap实例的方法
77
+ * @param id 地图容器的ID
78
+ * @param option 地图的配置选项
79
+ * @param mapType 地图的类型
80
+ */
81
+ static async create(id: string, option: option, mapType: string) {
82
+ // 判断是否在子路径下
83
+ const basePath = window.location.pathname.endsWith("/")
84
+ ? window.location.pathname
85
+ : window.location.pathname.substring(
86
+ 0,
87
+ window.location.pathname.lastIndexOf("/") + 1
88
+ );
89
+ await loadResource(basePath + "lib/turf/turf.min.js", "js");
88
90
 
89
- // 1.根据不同的地图类型,加载相应的资源
90
- switch (mapType) {
91
- case "mars3d":
91
+ // 1.根据不同的地图类型,加载相应的资源
92
+ switch (mapType) {
93
+ case "mars3d":
94
+ await loadResource(basePath + "lib/Cesium/Widgets/widgets.css", "css");
95
+ await loadResource(basePath + "lib/Cesium/Cesium.js", "js");
96
+ await loadResource(basePath + "lib/mars3d/mars3d.css", "css");
97
+ await loadResource(basePath + "lib/mars3d/mars3d.js", "js");
98
+ await loadResource(
99
+ basePath + "lib/mars3d/plugins/heatmap/heatmap.js",
100
+ "js"
101
+ );
102
+ await loadResource(
103
+ basePath + "lib/mars3d/plugins/heatmap/mars3d-heatmap.js",
104
+ "js"
105
+ );
106
+ break;
107
+ case "gaode":
108
+ await loadResource(
109
+ `https://webapi.amap.com/maps?v=2.0&key=${option.gaode_key}&plugin=AMap.HeatMap,AMap.MarkerCluster,AMap.MoveAnimation`,
110
+ "js"
111
+ );
112
+ await loadResource(
113
+ "https://a.amap.com/jsapi_demos/static/data3d/lib/three.117.js",
114
+ "js"
115
+ );
116
+ await loadResource(
117
+ "https://a.amap.com/jsapi_demos/static/data3d/lib/GLTFLoader.117.min.js",
118
+ "js"
119
+ );
120
+ break;
121
+ case "siji":
122
+ await loadResource(option.sj_js_url, "js");
123
+ await SGMap.tokenTask.login(option.sj_app_key, option.sj_app_secret);
124
+ await SGMap.plugin([
125
+ "SGMap.DrawPolygonHandler",
126
+ "SGMap.DrawCircleHandler",
127
+ "SGMap.DrawRectangleHandler",
128
+ "SGMap.GeocodingTask",
129
+ "SGMap.RoadNetLayer",
130
+ ]);
92
131
 
93
- await loadResource(basePath + "lib/Cesium/Widgets/widgets.css", "css");
94
- await loadResource(basePath + "lib/Cesium/Cesium.js", "js");
95
- await loadResource(basePath + "lib/mars3d/mars3d.css", "css");
96
- await loadResource(basePath + "lib/mars3d/mars3d.js", "js");
97
- await loadResource(
98
- basePath + "lib/mars3d/plugins/heatmap/heatmap.js",
99
- "js"
100
- );
101
- await loadResource(
102
- basePath + "lib/mars3d/plugins/heatmap/mars3d-heatmap.js",
103
- "js"
104
- );
105
- break;
106
- case "gaode":
107
- await loadResource(
108
- `https://webapi.amap.com/maps?v=2.0&key=${option.gaode_key}&plugin=AMap.HeatMap,AMap.MarkerCluster,AMap.MoveAnimation`,
109
- "js"
110
- );
111
- await loadResource(
112
- "https://a.amap.com/jsapi_demos/static/data3d/lib/three.117.js",
113
- "js"
114
- );
115
- await loadResource(
116
- "https://a.amap.com/jsapi_demos/static/data3d/lib/GLTFLoader.117.min.js",
117
- "js"
118
- );
119
- break;
120
- case "siji":
121
- await loadResource(option.sj_js_url, "js");
122
- await SGMap.tokenTask.login(option.sj_app_key, option.sj_app_secret);
123
- await SGMap.plugin([
124
- "SGMap.DrawPolygonHandler",
125
- "SGMap.DrawCircleHandler",
126
- "SGMap.DrawRectangleHandler",
127
- "SGMap.GeocodingTask",
128
- ]);
129
- break;
130
- }
132
+ break;
133
+ }
131
134
 
132
- // 2. 创建地图对象
133
- const MapClass = map({id, option, mapType}); // 假设 map 是一个工厂
134
- const innerMap = await MapClass.create(id, option); // 真正的地图实例
135
- // 3. 创建 HnMap 实例(此时传入 map)
136
- HnMap.allowConstruction = true;
137
- try {
138
- const instance = new HnMap(id, option, mapType, innerMap);
135
+ // 2. 创建地图对象
136
+ const MapClass = map({ id, option, mapType }); // 假设 map 是一个工厂
137
+ const innerMap = await MapClass.create(id, option); // 真正的地图实例
138
+ // 3. 创建 HnMap 实例(此时传入 map)
139
+ HnMap.allowConstruction = true;
140
+ try {
141
+ const instance = new HnMap(id, option, mapType, innerMap);
139
142
 
140
- // 4. ✅ 此时 this.map 已存在,再初始化所有模块
141
- instance.initModules();
142
- return instance;
143
- } finally {
144
- HnMap.allowConstruction = false;
145
- }
143
+ // 4. ✅ 此时 this.map 已存在,再初始化所有模块
144
+ instance.initModules();
145
+ return instance;
146
+ } finally {
147
+ HnMap.allowConstruction = false;
146
148
  }
149
+ }
147
150
 
148
- // ✅ 添加一个初始化方法,用于注册所有图形类
149
- private initModules() {
150
- this.Layer = layer(this);
151
- this.Point = point(this);
152
- this.NumPoint = numPoint(this);
153
- this.ImagePoint = imagePoint(this);
154
- this.DivPoint = divPoint(this);
155
- this.Label = label(this);
156
- this.Line = line(this);
157
- this.Circle = circle(this);
158
- this.Rectangle = rectangle(this);
159
- this.Polygon = polygon(this);
160
- this.Route = route(this);
161
- this.HeatMap = heatMap(this);
162
- this.Cluster = cluster(this);
163
- this.PointCloud = pointCloud(this);
164
- this.GeoJson = geoJson(this);
165
- }
151
+ // ✅ 添加一个初始化方法,用于注册所有图形类
152
+ private initModules() {
153
+ this.Layer = layer(this);
154
+ this.Point = point(this);
155
+ this.NumPoint = numPoint(this);
156
+ this.ImagePoint = imagePoint(this);
157
+ this.DivPoint = divPoint(this);
158
+ this.Label = label(this);
159
+ this.Line = line(this);
160
+ this.Circle = circle(this);
161
+ this.Rectangle = rectangle(this);
162
+ this.Polygon = polygon(this);
163
+ this.Route = route(this);
164
+ this.HeatMap = heatMap(this);
165
+ this.Cluster = cluster(this);
166
+ this.PointCloud = pointCloud(this);
167
+
168
+ this.GeoJson = geoJson(this);
169
+ }
166
170
  }
167
171
 
168
172
  /**
@@ -171,15 +175,15 @@ export default class HnMap {
171
175
  * @returns {boolean} 资源是否已加载
172
176
  */
173
177
  const isResourceLoaded = (path: string): boolean => {
174
- // 检查脚本
175
- const scripts = Array.from(document.getElementsByTagName("script"));
176
- if (scripts.some((script) => script.src.includes(path))) {
177
- return true;
178
- }
178
+ // 检查脚本
179
+ const scripts = Array.from(document.getElementsByTagName("script"));
180
+ if (scripts.some((script) => script.src.includes(path))) {
181
+ return true;
182
+ }
179
183
 
180
- // 检查样式
181
- const links = Array.from(document.getElementsByTagName("link"));
182
- return links.some((link) => link.href.includes(path));
184
+ // 检查样式
185
+ const links = Array.from(document.getElementsByTagName("link"));
186
+ return links.some((link) => link.href.includes(path));
183
187
  };
184
188
 
185
189
  /**
@@ -189,25 +193,25 @@ const isResourceLoaded = (path: string): boolean => {
189
193
  * @returns {Promise<void>} 加载完成的Promise
190
194
  */
191
195
  const loadResource = (path: string, type: string): Promise<void> => {
192
- return new Promise<void>((resolve, reject) => {
193
- // 如果资源已加载,直接返回
194
- if (isResourceLoaded(path)) {
195
- resolve();
196
- return;
197
- }
198
- if (type === "css") {
199
- const link = document.createElement("link");
200
- link.rel = "stylesheet";
201
- link.href = path;
202
- link.onload = () => resolve();
203
- link.onerror = reject;
204
- document.head.appendChild(link);
205
- } else {
206
- const script = document.createElement("script");
207
- script.src = path;
208
- script.onload = () => resolve();
209
- script.onerror = reject;
210
- document.body.appendChild(script);
211
- }
212
- });
196
+ return new Promise<void>((resolve, reject) => {
197
+ // 如果资源已加载,直接返回
198
+ if (isResourceLoaded(path)) {
199
+ resolve();
200
+ return;
201
+ }
202
+ if (type === "css") {
203
+ const link = document.createElement("link");
204
+ link.rel = "stylesheet";
205
+ link.href = path;
206
+ link.onload = () => resolve();
207
+ link.onerror = reject;
208
+ document.head.appendChild(link);
209
+ } else {
210
+ const script = document.createElement("script");
211
+ script.src = path;
212
+ script.onload = () => resolve();
213
+ script.onerror = reject;
214
+ document.body.appendChild(script);
215
+ }
216
+ });
213
217
  };