@hzab/map-combine 0.2.8 → 0.3.0-alpha.0

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,273 +1,273 @@
1
- import * as ol from "ol/index.js";
2
- import Map from "ol/Map.js";
3
- import View from "ol/View";
4
- import XYZ from "ol/source/XYZ";
5
- import Tile from "ol/layer/Tile";
6
- import DragPan from "ol/interaction/DragPan";
7
- import VectorSource from "ol/source/Vector";
8
- import VectorLayer from "ol/layer/Vector";
9
- import { fromLonLat } from "ol/proj";
10
- import * as olGeom from "ol/geom";
11
- import * as olSource from "ol/source";
12
- import * as olStyle from "ol/style";
13
- import * as olLayer from "ol/layer";
14
- import * as olExtent from "ol/extent";
15
-
16
- import { MapCombine } from "../basic/MapCombine";
17
- import { PointOption, Point } from "../basic/Point";
18
- import { Projection } from "../utils/Projection";
19
- import { bindEvent } from "./OpenlayerEvent";
20
- import { PolylineOption, Polyline } from "../basic/Polyline";
21
- import DoubleClickZoom from "ol/interaction/DoubleClickZoom";
22
- import { PolygonOption, Polygon } from "../basic/Polygon";
23
-
24
- import { drawPoint } from "./OpenlayerPoint";
25
- import { drawPolyline } from "./OpenlayerPolyline";
26
- import { drawPolygon } from "./OpenlayerPolygon";
27
- import { Tile3DOption, Tile3D } from "../basic/Tile3D";
28
- import { gcjMecator } from "./openlayer-tile-gcj02-wgs84";
29
-
30
- const MAX_ZOOM = 18;
31
-
32
- export interface IOpenlayerOption {
33
- container;
34
- url: string | string[];
35
- center?: number[];
36
- zoom?: number;
37
- maxZoom?: number;
38
- minZoom?: number;
39
- /** 地图瓦片转换规则 GCJ02 转 WGS84 */
40
- proj?: "GCJ02->WGS84";
41
- }
42
-
43
- export class OpenlayerMap extends MapCombine {
44
- viewer: Map;
45
- container: HTMLDivElement;
46
- dragPan: DragPan;
47
- vectors: VectorSource;
48
- option: IOpenlayerOption;
49
- offset: number[];
50
- fov = 0.9272952180016121;
51
- isFromLonLat = true;
52
- get cursor(): string {
53
- return this.container.style.cursor;
54
- }
55
- set cursor(val: string) {
56
- this.container.style.cursor = val;
57
- }
58
- get moveable(): boolean {
59
- return this.dragPan.getActive();
60
- }
61
- set moveable(val: boolean) {
62
- this.dragPan.setActive(val);
63
- }
64
- get center(): number[] {
65
- const center = this.viewer.getView().getCenter()!;
66
- return [Projection.xToLon(center[0]), Projection.yToLat(center[1]), 0];
67
- }
68
- set center(val: number[]) {
69
- const { viewer } = this;
70
- const view = viewer.getView();
71
- view.setCenter([Projection.lonToX(val[0]), Projection.latToY(val[1])]);
72
- }
73
- get zoom(): number {
74
- return this.viewer.getView().getZoom();
75
- }
76
- set zoom(val: number) {
77
- this.viewer.getView().setZoom(val);
78
- }
79
-
80
- constructor(viewer?: Map) {
81
- super();
82
- viewer && this._initParamsEvent(viewer);
83
- }
84
-
85
- init(opt: IOpenlayerOption) {
86
- this.option = opt;
87
- const { container, center = [120.2288892, 30.2349677, 0], zoom = 13, url, maxZoom, minZoom, proj } = opt || {};
88
-
89
- const viewer = new Map({
90
- target: container,
91
- controls: [],
92
- view: new View({
93
- projection: "EPSG:3857",
94
- center: fromLonLat(center),
95
- zoom: zoom,
96
- maxZoom: maxZoom ?? 18,
97
- minZoom: minZoom ?? 3,
98
- }),
99
- layers: [
100
- new Tile({
101
- visible: true,
102
- source: new XYZ({
103
- projection: proj === "GCJ02->WGS84" ? gcjMecator : undefined,
104
- urls: Array.isArray(url) ? url : [url],
105
- crossOrigin: "anonymous",
106
- }),
107
- }),
108
- ],
109
- });
110
-
111
- this._initParamsEvent(viewer);
112
-
113
- return new OpenlayerMap(viewer);
114
- }
115
-
116
- private _initParamsEvent(viewer) {
117
- if (!viewer) {
118
- return;
119
- }
120
- this.viewer = viewer;
121
- this.container = viewer.getViewport() as HTMLDivElement;
122
- this.container.appendChild(this.htmllayer);
123
- this.cursor = "default";
124
-
125
- const { center } = this;
126
- const proj = this.viewer.getView().getProjection().getCode();
127
- this.isFromLonLat = proj === "EPSG:3857" || proj === "EPSG:4326";
128
- this.offset = this.getFromLonLat(center);
129
- this.vectors = new VectorSource();
130
- this.viewer.addLayer(
131
- new VectorLayer({
132
- source: this.vectors,
133
- }),
134
- );
135
-
136
- this.dragPan = viewer
137
- .getInteractions()
138
- .getArray()
139
- .find((interaction) => {
140
- if (interaction instanceof DoubleClickZoom) {
141
- interaction.setActive(false);
142
- }
143
- return interaction instanceof DragPan;
144
- }) as DragPan;
145
-
146
- bindEvent(this);
147
- }
148
-
149
- getFromLonLat(lonLat, isFromLonLat = this.isFromLonLat) {
150
- return isFromLonLat ? fromLonLat(lonLat) : lonLat;
151
- }
152
-
153
- createPoint<K>(option?: Partial<PointOption<K>>): Point<K> {
154
- const e = new Point(this, option);
155
- drawPoint(this, e);
156
- return e;
157
- }
158
-
159
- createPolyline<K>(option?: Partial<PolylineOption<K>>): Polyline<K> {
160
- const e = new Polyline(this, option);
161
- drawPolyline(this, e);
162
- return e;
163
- }
164
- createPolygon<K>(option?: Partial<PolygonOption<K>>): Polygon<K> {
165
- const e = new Polygon(this, option);
166
- drawPolygon(this, e);
167
- return e;
168
- }
169
-
170
- // protected _onFrame(): void {
171
- // const { viewer, camera, offset } = this;
172
- // const box = viewer.getView().calculateExtent(viewer.getSize());
173
-
174
- // const height = box[3] - box[1];
175
-
176
- // camera.position.set((box[2] + box[0]) / 2 - offset[0], (box[3] + box[1]) / 2 - offset[1], height);
177
-
178
- // // this.meterPerPixel = height / this.container.offsetHeight;
179
- // }
180
-
181
- /**
182
- * 所有点位移动至视口,调整位置及层级
183
- * @param points
184
- * @param opt
185
- * @param {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
186
- */
187
- flyToViewer(points, opt = { avoid: [0, 0, 0, 0], maxZoom: MAX_ZOOM }) {
188
- if (points?.length === 0) {
189
- return this.flyTo(points[0], opt);
190
- }
191
- const { avoid } = opt || {};
192
- // 创建 Feature 数组
193
- const features = points.map((point) => {
194
- const [lon, lat] = point;
195
- const coord = this.getFromLonLat([lon, lat]);
196
- return new ol.Feature({
197
- geometry: new olGeom.Point(coord),
198
- });
199
- });
200
-
201
- // 创建 VectorSource
202
- const source = new olSource.Vector({
203
- features: features,
204
- });
205
-
206
- // 计算边界范围
207
- const extent = source.getExtent();
208
-
209
- // 调整边界范围
210
- olExtent.buffer(extent, this.viewer.getView().getResolution(), extent);
211
-
212
- // 设置地图视图
213
- this.viewer.getView().fit(extent, {
214
- // avoid {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
215
- // 上 右 下 左
216
- padding: [avoid[0], avoid[3], avoid[1], avoid[2]],
217
- duration: 1000,
218
- maxZoom: opt?.maxZoom ?? MAX_ZOOM, // 最大缩放级别,
219
- ...opt,
220
- });
221
- }
222
-
223
- /**
224
- * 移动点位到中心点
225
- * @param point
226
- */
227
- flyTo(point, opt = {}) {
228
- // 设置相机视角
229
- this.viewer.getView().fit(point, {
230
- duration: 1000,
231
- ...opt,
232
- });
233
- }
234
-
235
- geographyTspace(p: number[]): number[] {
236
- const { offset } = this;
237
- return [Projection.lonToX(p[0]) - offset[0], Projection.latToY(p[1]) - offset[1], p[2] ?? 0];
238
- }
239
- geographyTcanvas(p: number[]): number[] {
240
- const pixel = this.viewer.getPixelFromCoordinate(this.getFromLonLat(p));
241
- if (pixel) {
242
- return [pixel[0], pixel[1]];
243
- } else {
244
- return [0, 0];
245
- }
246
- }
247
- spaceTgeography(p: number[]): number[] {
248
- const { offset } = this;
249
- return [Projection.xToLon(p[0] + offset[0]), Projection.yToLat(p[1] + offset[1]), p[2] ?? 0];
250
- }
251
- spaceTcanvas(p: number[]): number[] {
252
- const { offset } = this;
253
- const pixel = this.viewer.getPixelFromCoordinate([p[0] + offset[0], p[1] + offset[1]]);
254
- return [pixel[0], pixel[1]];
255
- }
256
- canvasTspace(p: number[]): number[] {
257
- const coordinates = this.viewer.getCoordinateFromPixel(p);
258
- return [coordinates[0] - this.offset[0], coordinates[1] - this.offset[1], 0];
259
- }
260
-
261
- createTile3D(option?: Partial<Tile3DOption>): Tile3D {
262
- throw new Error("Method not implemented.");
263
- }
264
-
265
- canvasTgeography(p: number[]): number[] {
266
- const coordinates = this.viewer.getCoordinateFromPixel(p);
267
- return [Projection.xToLon(coordinates[0]), Projection.yToLat(coordinates[1]), 0];
268
- }
269
-
270
- _onDestroy() {
271
- this.viewer.dispose();
272
- }
273
- }
1
+ import * as ol from "ol/index.js";
2
+ import Map from "ol/Map.js";
3
+ import View from "ol/View";
4
+ import XYZ from "ol/source/XYZ";
5
+ import Tile from "ol/layer/Tile";
6
+ import DragPan from "ol/interaction/DragPan";
7
+ import VectorSource from "ol/source/Vector";
8
+ import VectorLayer from "ol/layer/Vector";
9
+ import { fromLonLat } from "ol/proj";
10
+ import * as olGeom from "ol/geom";
11
+ import * as olSource from "ol/source";
12
+ import * as olStyle from "ol/style";
13
+ import * as olLayer from "ol/layer";
14
+ import * as olExtent from "ol/extent";
15
+
16
+ import { MapCombine } from "../basic/MapCombine";
17
+ import { PointOption, Point } from "../basic/Point";
18
+ import { Projection } from "../utils/Projection";
19
+ import { bindEvent } from "./OpenlayerEvent";
20
+ import { PolylineOption, Polyline } from "../basic/Polyline";
21
+ import DoubleClickZoom from "ol/interaction/DoubleClickZoom";
22
+ import { PolygonOption, Polygon } from "../basic/Polygon";
23
+
24
+ import { drawPoint } from "./OpenlayerPoint";
25
+ import { drawPolyline } from "./OpenlayerPolyline";
26
+ import { drawPolygon } from "./OpenlayerPolygon";
27
+ import { Tile3DOption, Tile3D } from "../basic/Tile3D";
28
+ import { gcjMecator } from "./openlayer-tile-gcj02-wgs84";
29
+
30
+ const MAX_ZOOM = 18;
31
+
32
+ export interface IOpenlayerOption {
33
+ container;
34
+ url: string | string[];
35
+ center?: number[];
36
+ zoom?: number;
37
+ maxZoom?: number;
38
+ minZoom?: number;
39
+ /** 地图瓦片转换规则 GCJ02 转 WGS84 */
40
+ proj?: "GCJ02->WGS84";
41
+ }
42
+
43
+ export class OpenlayerMap extends MapCombine {
44
+ viewer: Map;
45
+ container: HTMLDivElement;
46
+ dragPan: DragPan;
47
+ vectors: VectorSource;
48
+ option: IOpenlayerOption;
49
+ offset: number[];
50
+ fov = 0.9272952180016121;
51
+ isFromLonLat = true;
52
+ get cursor(): string {
53
+ return this.container.style.cursor;
54
+ }
55
+ set cursor(val: string) {
56
+ this.container.style.cursor = val;
57
+ }
58
+ get moveable(): boolean {
59
+ return this.dragPan.getActive();
60
+ }
61
+ set moveable(val: boolean) {
62
+ this.dragPan.setActive(val);
63
+ }
64
+ get center(): number[] {
65
+ const center = this.viewer.getView().getCenter()!;
66
+ return [Projection.xToLon(center[0]), Projection.yToLat(center[1]), 0];
67
+ }
68
+ set center(val: number[]) {
69
+ const { viewer } = this;
70
+ const view = viewer.getView();
71
+ view.setCenter([Projection.lonToX(val[0]), Projection.latToY(val[1])]);
72
+ }
73
+ get zoom(): number {
74
+ return this.viewer.getView().getZoom();
75
+ }
76
+ set zoom(val: number) {
77
+ this.viewer.getView().setZoom(val);
78
+ }
79
+
80
+ constructor(viewer?: Map) {
81
+ super();
82
+ viewer && this._initParamsEvent(viewer);
83
+ }
84
+
85
+ init(opt: IOpenlayerOption) {
86
+ this.option = opt;
87
+ const { container, center = [120.2288892, 30.2349677, 0], zoom = 13, url, maxZoom, minZoom, proj } = opt || {};
88
+
89
+ const viewer = new Map({
90
+ target: container,
91
+ controls: [],
92
+ view: new View({
93
+ projection: "EPSG:3857",
94
+ center: fromLonLat(center),
95
+ zoom: zoom,
96
+ maxZoom: maxZoom ?? 18,
97
+ minZoom: minZoom ?? 3,
98
+ }),
99
+ layers: [
100
+ new Tile({
101
+ visible: true,
102
+ source: new XYZ({
103
+ projection: proj === "GCJ02->WGS84" ? gcjMecator : undefined,
104
+ urls: Array.isArray(url) ? url : [url],
105
+ crossOrigin: "anonymous",
106
+ }),
107
+ }),
108
+ ],
109
+ });
110
+
111
+ this._initParamsEvent(viewer);
112
+
113
+ return new OpenlayerMap(viewer);
114
+ }
115
+
116
+ private _initParamsEvent(viewer) {
117
+ if (!viewer) {
118
+ return;
119
+ }
120
+ this.viewer = viewer;
121
+ this.container = viewer.getViewport() as HTMLDivElement;
122
+ this.container.appendChild(this.htmllayer);
123
+ this.cursor = "default";
124
+
125
+ const { center } = this;
126
+ const proj = this.viewer.getView().getProjection().getCode();
127
+ this.isFromLonLat = proj === "EPSG:3857" || proj === "EPSG:4326";
128
+ this.offset = this.getFromLonLat(center);
129
+ this.vectors = new VectorSource();
130
+ this.viewer.addLayer(
131
+ new VectorLayer({
132
+ source: this.vectors,
133
+ }),
134
+ );
135
+
136
+ this.dragPan = viewer
137
+ .getInteractions()
138
+ .getArray()
139
+ .find((interaction) => {
140
+ if (interaction instanceof DoubleClickZoom) {
141
+ interaction.setActive(false);
142
+ }
143
+ return interaction instanceof DragPan;
144
+ }) as DragPan;
145
+
146
+ bindEvent(this);
147
+ }
148
+
149
+ getFromLonLat(lonLat, isFromLonLat = this.isFromLonLat) {
150
+ return isFromLonLat ? fromLonLat(lonLat) : lonLat;
151
+ }
152
+
153
+ createPoint<K>(option?: Partial<PointOption<K>>): Point<K> {
154
+ const e = new Point(this, option);
155
+ drawPoint(this, e);
156
+ return e;
157
+ }
158
+
159
+ createPolyline<K>(option?: Partial<PolylineOption<K>>): Polyline<K> {
160
+ const e = new Polyline(this, option);
161
+ drawPolyline(this, e);
162
+ return e;
163
+ }
164
+ createPolygon<K>(option?: Partial<PolygonOption<K>>): Polygon<K> {
165
+ const e = new Polygon(this, option);
166
+ drawPolygon(this, e);
167
+ return e;
168
+ }
169
+
170
+ // protected _onFrame(): void {
171
+ // const { viewer, camera, offset } = this;
172
+ // const box = viewer.getView().calculateExtent(viewer.getSize());
173
+
174
+ // const height = box[3] - box[1];
175
+
176
+ // camera.position.set((box[2] + box[0]) / 2 - offset[0], (box[3] + box[1]) / 2 - offset[1], height);
177
+
178
+ // // this.meterPerPixel = height / this.container.offsetHeight;
179
+ // }
180
+
181
+ /**
182
+ * 所有点位移动至视口,调整位置及层级
183
+ * @param points
184
+ * @param opt
185
+ * @param {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
186
+ */
187
+ flyToViewer(points, opt = { avoid: [0, 0, 0, 0], maxZoom: MAX_ZOOM }) {
188
+ if (points?.length === 0) {
189
+ return this.flyTo(points[0], opt);
190
+ }
191
+ const { avoid } = opt || {};
192
+ // 创建 Feature 数组
193
+ const features = points.map((point) => {
194
+ const [lon, lat] = point;
195
+ const coord = this.getFromLonLat([lon, lat]);
196
+ return new ol.Feature({
197
+ geometry: new olGeom.Point(coord),
198
+ });
199
+ });
200
+
201
+ // 创建 VectorSource
202
+ const source = new olSource.Vector({
203
+ features: features,
204
+ });
205
+
206
+ // 计算边界范围
207
+ const extent = source.getExtent();
208
+
209
+ // 调整边界范围
210
+ olExtent.buffer(extent, this.viewer.getView().getResolution(), extent);
211
+
212
+ // 设置地图视图
213
+ this.viewer.getView().fit(extent, {
214
+ // avoid {Array<number> = [0,0,0,0]} opt.avoid 距离边框的内边距,顺序:上、下、左、右
215
+ // 上 右 下 左
216
+ padding: [avoid[0], avoid[3], avoid[1], avoid[2]],
217
+ duration: 1000,
218
+ maxZoom: opt?.maxZoom ?? MAX_ZOOM, // 最大缩放级别,
219
+ ...opt,
220
+ });
221
+ }
222
+
223
+ /**
224
+ * 移动点位到中心点
225
+ * @param point
226
+ */
227
+ flyTo(point, opt = {}) {
228
+ // 设置相机视角
229
+ this.viewer.getView().fit(point, {
230
+ duration: 1000,
231
+ ...opt,
232
+ });
233
+ }
234
+
235
+ geographyTspace(p: number[]): number[] {
236
+ const { offset } = this;
237
+ return [Projection.lonToX(p[0]) - offset[0], Projection.latToY(p[1]) - offset[1], p[2] ?? 0];
238
+ }
239
+ geographyTcanvas(p: number[]): number[] {
240
+ const pixel = this.viewer.getPixelFromCoordinate(this.getFromLonLat(p));
241
+ if (pixel) {
242
+ return [pixel[0], pixel[1]];
243
+ } else {
244
+ return [0, 0];
245
+ }
246
+ }
247
+ spaceTgeography(p: number[]): number[] {
248
+ const { offset } = this;
249
+ return [Projection.xToLon(p[0] + offset[0]), Projection.yToLat(p[1] + offset[1]), p[2] ?? 0];
250
+ }
251
+ spaceTcanvas(p: number[]): number[] {
252
+ const { offset } = this;
253
+ const pixel = this.viewer.getPixelFromCoordinate([p[0] + offset[0], p[1] + offset[1]]);
254
+ return [pixel[0], pixel[1]];
255
+ }
256
+ canvasTspace(p: number[]): number[] {
257
+ const coordinates = this.viewer.getCoordinateFromPixel(p);
258
+ return [coordinates[0] - this.offset[0], coordinates[1] - this.offset[1], 0];
259
+ }
260
+
261
+ createTile3D(option?: Partial<Tile3DOption>): Tile3D {
262
+ throw new Error("Method not implemented.");
263
+ }
264
+
265
+ canvasTgeography(p: number[]): number[] {
266
+ const coordinates = this.viewer.getCoordinateFromPixel(p);
267
+ return [Projection.xToLon(coordinates[0]), Projection.yToLat(coordinates[1]), 0];
268
+ }
269
+
270
+ _onDestroy() {
271
+ this.viewer.dispose();
272
+ }
273
+ }