1 | import { __decorate, __metadata } from 'tslib';
|
2 | import { NgZone, Injectable, EventEmitter, Input, Output, Directive, NgModule } from '@angular/core';
|
3 | import { MarkerManager, GoogleMapsAPIWrapper, InfoWindowManager, AgmCoreModule } from '@agm/core';
|
4 | import { Observable } from 'rxjs';
|
5 | import 'js-marker-clusterer';
|
6 |
|
7 | let ClusterManager = class ClusterManager extends MarkerManager {
|
8 | constructor(_mapsWrapper, _zone) {
|
9 | super(_mapsWrapper, _zone);
|
10 | this._mapsWrapper = _mapsWrapper;
|
11 | this._zone = _zone;
|
12 | this._clustererInstance = new Promise((resolver) => {
|
13 | this._resolver = resolver;
|
14 | });
|
15 | }
|
16 | init(options) {
|
17 | this._mapsWrapper.getNativeMap().then(map => {
|
18 | const clusterer = new MarkerClusterer(map, [], options);
|
19 | this._resolver(clusterer);
|
20 | });
|
21 | }
|
22 | getClustererInstance() {
|
23 | return this._clustererInstance;
|
24 | }
|
25 | addMarker(marker) {
|
26 | const clusterPromise = this.getClustererInstance();
|
27 | const markerPromise = this._mapsWrapper
|
28 | .createMarker({
|
29 | position: {
|
30 | lat: marker.latitude,
|
31 | lng: marker.longitude,
|
32 | },
|
33 | label: marker.label,
|
34 | draggable: marker.draggable,
|
35 | icon: marker.iconUrl,
|
36 | opacity: marker.opacity,
|
37 | visible: marker.visible,
|
38 | zIndex: marker.zIndex,
|
39 | title: marker.title,
|
40 | clickable: marker.clickable,
|
41 | }, false);
|
42 | Promise
|
43 | .all([clusterPromise, markerPromise])
|
44 | .then(([cluster, marker]) => {
|
45 | return cluster.addMarker(marker);
|
46 | });
|
47 | this._markers.set(marker, markerPromise);
|
48 | }
|
49 | deleteMarker(marker) {
|
50 | const m = this._markers.get(marker);
|
51 | if (m == null) {
|
52 |
|
53 | return Promise.resolve();
|
54 | }
|
55 | return m.then((m) => {
|
56 | this._zone.run(() => {
|
57 | m.setMap(null);
|
58 | this.getClustererInstance().then(cluster => {
|
59 | cluster.removeMarker(m);
|
60 | this._markers.delete(marker);
|
61 | });
|
62 | });
|
63 | });
|
64 | }
|
65 | clearMarkers() {
|
66 | return this.getClustererInstance().then(cluster => {
|
67 | cluster.clearMarkers();
|
68 | });
|
69 | }
|
70 | setGridSize(c) {
|
71 | this.getClustererInstance().then(cluster => {
|
72 | cluster.setGridSize(c.gridSize);
|
73 | });
|
74 | }
|
75 | setMaxZoom(c) {
|
76 | this.getClustererInstance().then(cluster => {
|
77 | cluster.setMaxZoom(c.maxZoom);
|
78 | });
|
79 | }
|
80 | setStyles(c) {
|
81 | this.getClustererInstance().then(cluster => {
|
82 | cluster.setStyles(c.styles);
|
83 | });
|
84 | }
|
85 | setZoomOnClick(c) {
|
86 | this.getClustererInstance().then(cluster => {
|
87 | if (c.zoomOnClick !== undefined) {
|
88 | cluster.zoomOnClick_ = c.zoomOnClick;
|
89 | }
|
90 | });
|
91 | }
|
92 | setAverageCenter(c) {
|
93 | this.getClustererInstance().then(cluster => {
|
94 | if (c.averageCenter !== undefined) {
|
95 | cluster.averageCenter_ = c.averageCenter;
|
96 | }
|
97 | });
|
98 | }
|
99 | setImagePath(c) {
|
100 | this.getClustererInstance().then(cluster => {
|
101 | if (c.imagePath !== undefined) {
|
102 | cluster.imagePath_ = c.imagePath;
|
103 | }
|
104 | });
|
105 | }
|
106 | setMinimumClusterSize(c) {
|
107 | this.getClustererInstance().then(cluster => {
|
108 | if (c.minimumClusterSize !== undefined) {
|
109 | cluster.minimumClusterSize_ = c.minimumClusterSize;
|
110 | }
|
111 | });
|
112 | }
|
113 | setImageExtension(c) {
|
114 | this.getClustererInstance().then(cluster => {
|
115 | if (c.imageExtension !== undefined) {
|
116 | cluster.imageExtension_ = c.imageExtension;
|
117 | }
|
118 | });
|
119 | }
|
120 | createClusterEventObservable(eventName) {
|
121 | return Observable.create((observer) => {
|
122 | this._zone.runOutsideAngular(() => {
|
123 | this._clustererInstance.then((m) => {
|
124 | m.addListener(eventName, (e) => this._zone.run(() => observer.next(e)));
|
125 | });
|
126 | });
|
127 | });
|
128 | }
|
129 | setCalculator(c) {
|
130 | this.getClustererInstance().then(cluster => {
|
131 | if (typeof c.calculator === 'function') {
|
132 | cluster.setCalculator(c.calculator);
|
133 | }
|
134 | });
|
135 | }
|
136 | };
|
137 | ClusterManager.ctorParameters = () => [
|
138 | { type: GoogleMapsAPIWrapper },
|
139 | { type: NgZone }
|
140 | ];
|
141 | ClusterManager = __decorate([
|
142 | Injectable(),
|
143 | __metadata("design:paramtypes", [GoogleMapsAPIWrapper, NgZone])
|
144 | ], ClusterManager);
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | let AgmMarkerCluster = class AgmMarkerCluster {
|
174 | constructor(_clusterManager) {
|
175 | this._clusterManager = _clusterManager;
|
176 | this.clusterClick = new EventEmitter();
|
177 | this._observableSubscriptions = [];
|
178 | }
|
179 |
|
180 | ngOnDestroy() {
|
181 | this._clusterManager.clearMarkers();
|
182 | this._observableSubscriptions.forEach((s) => s.unsubscribe());
|
183 | }
|
184 |
|
185 | ngOnChanges(changes) {
|
186 | if (changes['gridSize']) {
|
187 | this._clusterManager.setGridSize(this);
|
188 | }
|
189 | if (changes['maxZoom']) {
|
190 | this._clusterManager.setMaxZoom(this);
|
191 | }
|
192 | if (changes['zoomOnClick']) {
|
193 | this._clusterManager.setZoomOnClick(this);
|
194 | }
|
195 | if (changes['averageCenter']) {
|
196 | this._clusterManager.setAverageCenter(this);
|
197 | }
|
198 | if (changes['minimumClusterSize']) {
|
199 | this._clusterManager.setMinimumClusterSize(this);
|
200 | }
|
201 | if (changes['imagePath']) {
|
202 | this._clusterManager.setImagePath(this);
|
203 | }
|
204 | if (changes['imageExtension']) {
|
205 | this._clusterManager.setImageExtension(this);
|
206 | }
|
207 | if (changes['calculator']) {
|
208 | this._clusterManager.setCalculator(this);
|
209 | }
|
210 | if (changes['styles']) {
|
211 | this._clusterManager.setStyles(this);
|
212 | }
|
213 | }
|
214 | _addEventListeners() {
|
215 | const handlers = [
|
216 | {
|
217 | name: 'clusterclick',
|
218 | handler: () => this.clusterClick.emit(),
|
219 | },
|
220 | ];
|
221 | handlers.forEach((obj) => {
|
222 | const os = this._clusterManager.createClusterEventObservable(obj.name).subscribe(obj.handler);
|
223 | this._observableSubscriptions.push(os);
|
224 | });
|
225 | }
|
226 |
|
227 | ngOnInit() {
|
228 | this._addEventListeners();
|
229 | this._clusterManager.init({
|
230 | gridSize: this.gridSize,
|
231 | maxZoom: this.maxZoom,
|
232 | zoomOnClick: this.zoomOnClick,
|
233 | averageCenter: this.averageCenter,
|
234 | minimumClusterSize: this.minimumClusterSize,
|
235 | styles: this.styles,
|
236 | imagePath: this.imagePath,
|
237 | imageExtension: this.imageExtension,
|
238 | calculator: this.calculator,
|
239 | });
|
240 | }
|
241 | };
|
242 | AgmMarkerCluster.ctorParameters = () => [
|
243 | { type: ClusterManager }
|
244 | ];
|
245 | __decorate([
|
246 | Input(),
|
247 | __metadata("design:type", Number)
|
248 | ], AgmMarkerCluster.prototype, "gridSize", void 0);
|
249 | __decorate([
|
250 | Input(),
|
251 | __metadata("design:type", Number)
|
252 | ], AgmMarkerCluster.prototype, "maxZoom", void 0);
|
253 | __decorate([
|
254 | Input(),
|
255 | __metadata("design:type", Boolean)
|
256 | ], AgmMarkerCluster.prototype, "zoomOnClick", void 0);
|
257 | __decorate([
|
258 | Input(),
|
259 | __metadata("design:type", Boolean)
|
260 | ], AgmMarkerCluster.prototype, "averageCenter", void 0);
|
261 | __decorate([
|
262 | Input(),
|
263 | __metadata("design:type", Number)
|
264 | ], AgmMarkerCluster.prototype, "minimumClusterSize", void 0);
|
265 | __decorate([
|
266 | Input(),
|
267 | __metadata("design:type", Array)
|
268 | ], AgmMarkerCluster.prototype, "styles", void 0);
|
269 | __decorate([
|
270 | Input(),
|
271 | __metadata("design:type", Function)
|
272 | ], AgmMarkerCluster.prototype, "calculator", void 0);
|
273 | __decorate([
|
274 | Input(),
|
275 | __metadata("design:type", String)
|
276 | ], AgmMarkerCluster.prototype, "imagePath", void 0);
|
277 | __decorate([
|
278 | Input(),
|
279 | __metadata("design:type", String)
|
280 | ], AgmMarkerCluster.prototype, "imageExtension", void 0);
|
281 | __decorate([
|
282 | Output(),
|
283 | __metadata("design:type", EventEmitter)
|
284 | ], AgmMarkerCluster.prototype, "clusterClick", void 0);
|
285 | AgmMarkerCluster = __decorate([
|
286 | Directive({
|
287 | selector: 'agm-marker-cluster',
|
288 | providers: [
|
289 | ClusterManager,
|
290 | { provide: MarkerManager, useExisting: ClusterManager },
|
291 | InfoWindowManager,
|
292 | ],
|
293 | }),
|
294 | __metadata("design:paramtypes", [ClusterManager])
|
295 | ], AgmMarkerCluster);
|
296 |
|
297 | let AgmJsMarkerClustererModule = class AgmJsMarkerClustererModule {
|
298 | };
|
299 | AgmJsMarkerClustererModule = __decorate([
|
300 | NgModule({
|
301 | imports: [AgmCoreModule],
|
302 | declarations: [AgmMarkerCluster],
|
303 | exports: [AgmMarkerCluster],
|
304 | })
|
305 | ], AgmJsMarkerClustererModule);
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | export { AgmJsMarkerClustererModule, AgmMarkerCluster, ClusterManager };
|
314 |
|