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/dist/index.js +78 -21
- package/package.json +1 -1
- package/src/base/siji_entity.ts +22 -15
- package/src/graphic/imagePoint.ts +38 -7
- package/src/graphic/numPoint.ts +12 -8
- package/src/graphic/point.ts +2 -2
- package/src/index.ts +164 -160
- package/src/layer/layer.ts +430 -415
- package/src/map.ts +394 -388
- package/src/other/geocodingTask.ts +120 -0
package/src/index.ts
CHANGED
|
@@ -19,150 +19,154 @@ import geoJson from "./layer/geoJson";
|
|
|
19
19
|
* HnMap类是一个地图工具类,用于创建和管理地图对象及其相关元素
|
|
20
20
|
*/
|
|
21
21
|
interface option {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
94
|
-
|
|
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
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
143
|
+
// 4. ✅ 此时 this.map 已存在,再初始化所有模块
|
|
144
|
+
instance.initModules();
|
|
145
|
+
return instance;
|
|
146
|
+
} finally {
|
|
147
|
+
HnMap.allowConstruction = false;
|
|
146
148
|
}
|
|
149
|
+
}
|
|
147
150
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
-
|
|
182
|
-
|
|
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
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
};
|