-
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(markers-for-cluster): new component markers-for-cluster
This new component doesn't need direct access to supercluster Deprecate mgl-marker-cluster
- Loading branch information
Showing
5 changed files
with
121 additions
and
12 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
110 changes: 110 additions & 0 deletions
110
projects/ngx-mapbox-gl/src/lib/markers-for-clusters/markers-for-clusters.component.ts
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,110 @@ | ||
import { | ||
AfterContentInit, | ||
ChangeDetectionStrategy, | ||
ChangeDetectorRef, | ||
Component, | ||
ContentChild, | ||
Directive, | ||
Input, | ||
NgZone, | ||
OnDestroy, | ||
TemplateRef | ||
} from '@angular/core'; | ||
import { MapboxGeoJSONFeature, MapSourceDataEvent } from 'mapbox-gl'; | ||
import { fromEvent, merge, Subscription } from 'rxjs'; | ||
import { filter, startWith, switchMap, take } from 'rxjs/operators'; | ||
import { MapService } from '../map/map.service'; | ||
|
||
@Directive({ selector: 'ng-template[mglPoint]' }) | ||
export class PointDirective { } | ||
|
||
@Directive({ selector: 'ng-template[mglClusterPoint]' }) | ||
export class ClusterPointDirective { } | ||
|
||
let uniqId = 0; | ||
|
||
@Component({ | ||
selector: 'mgl-markers-for-clusters', | ||
template: ` | ||
<mgl-layer | ||
[id]="layerId" | ||
[source]="source" | ||
type="circle" | ||
[paint]="{'circle-radius': 0}" | ||
></mgl-layer> | ||
<ng-container *ngFor="let feature of clusterPoints; trackBy: trackByClusterPoint"> | ||
<ng-container *ngIf="feature.properties.cluster"> | ||
<mgl-marker | ||
[feature]="feature" | ||
> | ||
<ng-container *ngTemplateOutlet="clusterPointTpl; context: { $implicit: feature }"></ng-container> | ||
</mgl-marker> | ||
</ng-container> | ||
<ng-container *ngIf="!feature.properties.cluster"> | ||
<mgl-marker | ||
[feature]="feature" | ||
> | ||
<ng-container *ngTemplateOutlet="pointTpl; context: { $implicit: feature }"></ng-container> | ||
</mgl-marker> | ||
</ng-container> | ||
</ng-container> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
preserveWhitespaces: false | ||
}) | ||
export class MarkersForClustersComponent implements OnDestroy, AfterContentInit { | ||
/* Init input */ | ||
@Input() source: string; | ||
|
||
@ContentChild(PointDirective, { read: TemplateRef }) pointTpl?: TemplateRef<any>; | ||
@ContentChild(ClusterPointDirective, { read: TemplateRef }) clusterPointTpl: TemplateRef<any>; | ||
|
||
clusterPoints: MapboxGeoJSONFeature[]; // Incorrect typings | ||
layerId = `mgl-markers-for-clusters-${uniqId++}`; | ||
|
||
private sub = new Subscription(); | ||
|
||
constructor( | ||
private MapService: MapService, | ||
private ChangeDetectorRef: ChangeDetectorRef, | ||
private zone: NgZone | ||
) { } | ||
|
||
ngAfterContentInit() { | ||
const sub = this.MapService.mapCreated$.pipe( | ||
switchMap(() => fromEvent<MapSourceDataEvent>(<any>this.MapService.mapInstance, 'data').pipe( | ||
filter((e) => e.sourceId === this.source && e.isSourceLoaded && e.sourceDataType !== 'metadata'), | ||
take(1) | ||
)), | ||
switchMap(() => merge( | ||
fromEvent(<any>this.MapService.mapInstance, 'move'), | ||
fromEvent(<any>this.MapService.mapInstance, 'moveend') | ||
).pipe( | ||
startWith<any>(undefined) | ||
)) | ||
).subscribe(() => { | ||
this.zone.run(() => { | ||
this.updateCluster(); | ||
}); | ||
}); | ||
this.sub.add(sub); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.sub.unsubscribe(); | ||
} | ||
|
||
trackByClusterPoint(_index: number, clusterPoint: { id: number }) { | ||
return clusterPoint.id; | ||
} | ||
|
||
private updateCluster() { | ||
// Invalid queryRenderedFeatures typing | ||
const params: any = { layers: [this.layerId] }; | ||
if (!this.pointTpl) { | ||
params.filter = ['==', 'cluster', true]; | ||
} | ||
this.clusterPoints = this.MapService.mapInstance.queryRenderedFeatures(params); | ||
this.ChangeDetectorRef.markForCheck(); | ||
} | ||
} |
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