-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cluster): add clusterComponent!
- Loading branch information
Wykks
committed
Dec 19, 2017
1 parent
390bf45
commit 1cb6daa
Showing
10 changed files
with
238 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.marker-cluster { | ||
width: 30px; | ||
height: 30px; | ||
border-radius: 50%; | ||
background-color: #4f615a; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
color: white; | ||
border: 2px solid #56C498; | ||
} | ||
|
||
.marker { | ||
width: 30px; | ||
height: 30px; | ||
border-radius: 50%; | ||
background-color: #7d7d7d; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
border: 2px solid #C9C9C9 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
const earthquakes = require('./earthquakes.geo.json'); | ||
|
||
@Component({ | ||
template: ` | ||
<mgl-map | ||
style="mapbox://styles/mapbox/dark-v9" | ||
[zoom]="[3]" | ||
[center]="[-103.59179687498357, 40.66995747013945]" | ||
> | ||
<mgl-cluster | ||
[data]="earthquakes" | ||
[maxZoom]="14" | ||
[radius]="50" | ||
> | ||
<ng-template mglPoint let-feature> | ||
<mgl-marker | ||
[feature]="feature" | ||
> | ||
<div | ||
class="marker" | ||
[title]="feature.properties['Secondary ID']" | ||
> | ||
{{ feature.properties['Primary ID'] }} | ||
</div> | ||
</mgl-marker> | ||
</ng-template> | ||
<ng-template mglClusterPoint let-feature> | ||
<mgl-marker | ||
[feature]="feature" | ||
> | ||
<div | ||
class="marker-cluster" | ||
> | ||
{{ feature.properties['point_count'] }} | ||
</div> | ||
</mgl-marker> | ||
</ng-template> | ||
</mgl-cluster> | ||
</mgl-map> | ||
`, | ||
styleUrls: ['./examples.css', './ngx-marker-cluster.component.css'] | ||
}) | ||
export class NgxMarkerClusterComponent { | ||
earthquakes = earthquakes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { merge } from 'rxjs/observable/merge'; | ||
import { fromEvent } from 'rxjs/observable/fromEvent'; | ||
import { startWith } from 'rxjs/operators/startWith'; | ||
import { Subscription } from 'rxjs/Subscription'; | ||
import supercluster, { Supercluster } from 'supercluster'; | ||
import { MapService } from '../map/map.service'; | ||
import { | ||
Component, | ||
Input, | ||
OnChanges, | ||
OnDestroy, | ||
SimpleChanges, | ||
OnInit, | ||
ChangeDetectionStrategy, | ||
Directive, | ||
TemplateRef, | ||
ContentChild, | ||
AfterContentInit, | ||
ChangeDetectorRef, | ||
} from '@angular/core'; | ||
|
||
@Directive({ selector: 'ng-template[mglPoint]' }) | ||
export class PointDirective { } | ||
|
||
@Directive({ selector: 'ng-template[mglClusterPoint]' }) | ||
export class ClusterPointDirective { } | ||
|
||
@Component({ | ||
selector: 'mgl-cluster', | ||
template: ` | ||
<ng-container *ngFor="let feature of clusterPoints"> | ||
<ng-container *ngIf="feature.properties.cluster; else point"> | ||
<ng-container *ngTemplateOutlet="clusterPointTpl; context: { $implicit: feature }"></ng-container> | ||
</ng-container> | ||
<ng-template #point> | ||
<ng-container *ngTemplateOutlet="pointTpl; context: { $implicit: feature }"></ng-container> | ||
</ng-template> | ||
</ng-container> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
preserveWhitespaces: false | ||
}) | ||
export class ClusterComponent implements OnChanges, OnDestroy, AfterContentInit, OnInit { | ||
/* Init input */ | ||
@Input() radius: any; | ||
@Input() maxZoom: any; | ||
@Input() minZoom: any; | ||
@Input() extent: any; | ||
@Input() nodeSize: any; | ||
@Input() log: any; | ||
|
||
/* Dynamic input */ | ||
@Input() data: GeoJSON.FeatureCollection<GeoJSON.Point>; | ||
|
||
@ContentChild(PointDirective, { read: TemplateRef }) pointTpl: TemplateRef<any>; | ||
@ContentChild(ClusterPointDirective, { read: TemplateRef }) clusterPointTpl: TemplateRef<any>; | ||
|
||
clusterPoints: GeoJSON.Feature<GeoJSON.Point>[]; | ||
|
||
private supercluster: Supercluster; | ||
private sub = new Subscription(); | ||
|
||
constructor( | ||
private MapService: MapService, | ||
private ChangeDetectorRef: ChangeDetectorRef | ||
) { } | ||
|
||
ngOnInit() { | ||
this.supercluster = supercluster({ | ||
radius: this.radius, | ||
maxZoom: this.maxZoom | ||
}); | ||
this.supercluster.load(this.data.features); | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges) { | ||
if (changes.data && !changes.data.isFirstChange()) { | ||
this.supercluster.load(this.data.features); | ||
} | ||
} | ||
|
||
ngAfterContentInit() { | ||
this.MapService.mapCreated$.subscribe(() => { | ||
const mapMove$ = merge( | ||
fromEvent(this.MapService.mapInstance, 'zoomChange'), | ||
fromEvent(this.MapService.mapInstance, 'move') | ||
); | ||
const sub = mapMove$.pipe( | ||
startWith<any>(undefined) | ||
).subscribe(() => this.updateCluster()); | ||
this.sub.add(sub); | ||
}); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.sub.unsubscribe(); | ||
} | ||
|
||
private updateCluster() { | ||
const bbox = this.MapService.getCurrentViewportBbox(); | ||
const currentZoom = Math.round(this.MapService.mapInstance.getZoom()); | ||
this.clusterPoints = this.supercluster.getClusters(bbox, currentZoom); | ||
this.ChangeDetectorRef.detectChanges(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters