Skip to content

Commit

Permalink
feat(markers-for-cluster): new component markers-for-cluster
Browse files Browse the repository at this point in the history
This new component doesn't need direct access to supercluster
Deprecate mgl-marker-cluster
  • Loading branch information
Wykks committed Jun 7, 2019
1 parent fd7dbec commit a5b97f6
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 12 deletions.
3 changes: 2 additions & 1 deletion projects/ngx-mapbox-gl/src/lib/map/map.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export class MapComponent implements OnChanges, OnDestroy, AfterViewInit, MapEve
/* Added by ngx-mapbox-gl */
@Input() movingMethod: 'jumpTo' | 'easeTo' | 'flyTo' = 'flyTo';
@Input() movingOptions?: MovingOptions;
@Input() fitBounds?: LngLatBoundsLike; // => First value is a alias to bounds input (since mapbox 0.53.0). Subsequents changes are passed to fitBounds
// => First value is a alias to bounds input (since mapbox 0.53.0). Subsequents changes are passed to fitBounds
@Input() fitBounds?: LngLatBoundsLike;
@Input() fitScreenCoordinates?: [PointLike, PointLike];
@Input() centerWithPanTo?: boolean;
@Input() panToOptions?: AnimationOptions;
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-mapbox-gl/src/lib/map/map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class MapService {
}

destroyMap() {
if(this.mapInstance) {
if (this.mapInstance) {
this.subscription.unsubscribe();
this.mapInstance.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ChangeDetectorRef,
Component,
ContentChild,
Directive,
EventEmitter,
Input,
NgZone,
Expand All @@ -19,12 +18,7 @@ import { fromEvent, merge, Subscription } from 'rxjs';
import { startWith } from 'rxjs/operators';
import Supercluster, { ClusterFeature, Options } from 'supercluster';
import { MapService } from '../map/map.service';

@Directive({ selector: 'ng-template[mglPoint]' })
export class PointDirective { }

@Directive({ selector: 'ng-template[mglClusterPoint]' })
export class ClusterPointDirective { }
import { ClusterPointDirective, PointDirective } from '../markers-for-clusters/markers-for-clusters.component';

@Component({
selector: 'mgl-marker-cluster',
Expand Down Expand Up @@ -85,6 +79,7 @@ export class MarkerClusterComponent implements OnChanges, OnDestroy, AfterConten
) { }

ngOnInit() {
console.warn('[ngx-mapbox-gl] mgl-marker-cluster is deprecated, use mgl-markers-for-clusters instead');
const options: Options<GeoJSON.GeoJsonProperties, GeoJSON.GeoJsonProperties> = {
radius: this.radius,
maxZoom: this.maxZoom,
Expand Down
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();
}
}
9 changes: 6 additions & 3 deletions projects/ngx-mapbox-gl/src/lib/ngx-mapbox-gl.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import { ImageComponent } from './image/image.component';
import { LayerComponent } from './layer/layer.component';
import { MapComponent } from './map/map.component';
import { MAPBOX_API_KEY } from './map/map.service';
import { ClusterPointDirective, MarkerClusterComponent, PointDirective } from './marker-cluster/marker-cluster.component';
import { MarkerClusterComponent } from './marker-cluster/marker-cluster.component';
import { MarkerComponent } from './marker/marker.component';
import { ClusterPointDirective, PointDirective, MarkersForClustersComponent } from './markers-for-clusters/markers-for-clusters.component';
import { PopupComponent } from './popup/popup.component';
import { CanvasSourceComponent } from './source/canvas-source.component';
import { FeatureComponent } from './source/geojson/feature.component';
Expand Down Expand Up @@ -50,7 +51,8 @@ import { VideoSourceComponent } from './source/video-source.component';
ScaleControlDirective,
PointDirective,
ClusterPointDirective,
MarkerClusterComponent
MarkerClusterComponent,
MarkersForClustersComponent
],
exports: [
MapComponent,
Expand All @@ -75,7 +77,8 @@ import { VideoSourceComponent } from './source/video-source.component';
ScaleControlDirective,
PointDirective,
ClusterPointDirective,
MarkerClusterComponent
MarkerClusterComponent,
MarkersForClustersComponent
]
})
export class NgxMapboxGLModule {
Expand Down

0 comments on commit a5b97f6

Please sign in to comment.