|
| 1 | +import {Directive, Input, OnDestroy, OnChanges, OnInit, SimpleChange} from '@angular/core'; |
| 2 | + |
| 3 | +import {ClusterManager} from '../services/managers/cluster-manager'; |
| 4 | +import {MarkerManager} from '@agm/core'; |
| 5 | + |
| 6 | +import {ClusterOptions, ClusterStyle} from '../services/google-clusterer-types'; |
| 7 | + |
| 8 | +/** |
| 9 | + * AgmMarkerCluster clusters map marker if they are near together |
| 10 | + * |
| 11 | + * ### Example |
| 12 | + * ```typescript |
| 13 | + * import { Component } from '@angular/core'; |
| 14 | + * |
| 15 | + * @Component({ |
| 16 | + * selector: 'my-map-cmp', |
| 17 | + * styles: [` |
| 18 | + * agm-map { |
| 19 | + * height: 300px; |
| 20 | + * } |
| 21 | + * `], |
| 22 | + * template: ` |
| 23 | + * <agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom"> |
| 24 | + * <agm-marker-cluster> |
| 25 | + * <agm-marker [latitude]="lat" [longitude]="lng" [label]="'M'"> |
| 26 | + * </agm-marker> |
| 27 | + * <agm-marker [latitude]="lat2" [longitude]="lng2" [label]="'N'"> |
| 28 | + * </agm-marker> |
| 29 | + * </agm-marker-cluster> |
| 30 | + * </agm-map> |
| 31 | + * ` |
| 32 | + * }) |
| 33 | + * ``` |
| 34 | + */ |
| 35 | +@Directive({ |
| 36 | + selector: 'agm-marker-cluster', |
| 37 | + providers: [ClusterManager, {provide: MarkerManager, useExisting: ClusterManager}] |
| 38 | +}) |
| 39 | +export class AgmMarkerCluster implements OnDestroy, OnChanges, OnInit, ClusterOptions { |
| 40 | + /** |
| 41 | + * The grid size of a cluster in pixels |
| 42 | + */ |
| 43 | + @Input() gridSize: number; |
| 44 | + |
| 45 | + /** |
| 46 | + * The maximum zoom level that a marker can be part of a cluster. |
| 47 | + */ |
| 48 | + @Input() maxZoom: number; |
| 49 | + |
| 50 | + /** |
| 51 | + * Whether the default behaviour of clicking on a cluster is to zoom into it. |
| 52 | + */ |
| 53 | + @Input() zoomOnClick: boolean; |
| 54 | + |
| 55 | + /** |
| 56 | + * Whether the center of each cluster should be the average of all markers in the cluster. |
| 57 | + */ |
| 58 | + @Input() averageCenter: boolean; |
| 59 | + |
| 60 | + /** |
| 61 | + * The minimum number of markers to be in a cluster before the markers are hidden and a count is shown. |
| 62 | + */ |
| 63 | + @Input() minimumClusterSize: number; |
| 64 | + |
| 65 | + /** |
| 66 | + * An object that has style properties. |
| 67 | + */ |
| 68 | + @Input() styles: ClusterStyle; |
| 69 | + |
| 70 | + @Input() imagePath: string; |
| 71 | + @Input() imageExtension: string; |
| 72 | + |
| 73 | + constructor(private _clusterManager: ClusterManager) {} |
| 74 | + |
| 75 | + /** @internal */ |
| 76 | + ngOnDestroy() { |
| 77 | + this._clusterManager.clearMarkers(); |
| 78 | + } |
| 79 | + |
| 80 | + /** @internal */ |
| 81 | + ngOnChanges(changes: {[key: string]: SimpleChange }) { |
| 82 | + if (changes['gridSize']) { |
| 83 | + this._clusterManager.setGridSize(this); |
| 84 | + } |
| 85 | + if (changes['maxZoom']) { |
| 86 | + this._clusterManager.setMaxZoom(this); |
| 87 | + } |
| 88 | + if (changes['styles']) { |
| 89 | + this._clusterManager.setStyles(this); |
| 90 | + } |
| 91 | + if (changes['zoomOnClick']) { |
| 92 | + this._clusterManager.setZoomOnClick(this); |
| 93 | + } |
| 94 | + if (changes['averageCenter']) { |
| 95 | + this._clusterManager.setAverageCenter(this); |
| 96 | + } |
| 97 | + if (changes['minimumClusterSize']) { |
| 98 | + this._clusterManager.setMinimumClusterSize(this); |
| 99 | + } |
| 100 | + if (changes['styles']) { |
| 101 | + this._clusterManager.setStyles(this); |
| 102 | + } |
| 103 | + if (changes['imagePath']) { |
| 104 | + this._clusterManager.setImagePath(this); |
| 105 | + } |
| 106 | + if (changes['imageExtension']) { |
| 107 | + this._clusterManager.setImageExtension(this); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** @internal */ |
| 112 | + ngOnInit() { |
| 113 | + this._clusterManager.init({ |
| 114 | + gridSize: this.gridSize, |
| 115 | + maxZoom: this.maxZoom, |
| 116 | + zoomOnClick: this.zoomOnClick, |
| 117 | + averageCenter: this.averageCenter, |
| 118 | + minimumClusterSize: this.minimumClusterSize, |
| 119 | + styles: this.styles, |
| 120 | + imagePath: this.imagePath, |
| 121 | + imageExtension: this.imageExtension, |
| 122 | + }); |
| 123 | + } |
| 124 | +} |
0 commit comments