Skip to content

Commit

Permalink
feat(markerCluster): expose supercluster by Ouput load event
Browse files Browse the repository at this point in the history
Update demo to use the new way of using markerCluster
Supercluster methods as template var is going to be removed in the next major version
  • Loading branch information
Wykks committed May 21, 2018
1 parent e708040 commit a87bd6f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 25 deletions.
7 changes: 6 additions & 1 deletion src/app/lib/marker-cluster/marker-cluster.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
OnInit,
SimpleChanges,
TemplateRef,
NgZone
NgZone,
Output,
EventEmitter
} from '@angular/core';
import { fromEvent } from 'rxjs/observable/fromEvent';
import { merge } from 'rxjs/observable/merge';
Expand Down Expand Up @@ -69,6 +71,8 @@ export class MarkerClusterComponent implements OnChanges, OnDestroy, AfterConten
/* Dynamic input */
@Input() data: GeoJSON.FeatureCollection<GeoJSON.Point>;

@Output() load = new EventEmitter<Supercluster>();

@ContentChild(PointDirective, { read: TemplateRef }) pointTpl: TemplateRef<any>;
@ContentChild(ClusterPointDirective, { read: TemplateRef }) clusterPointTpl: TemplateRef<any>;

Expand Down Expand Up @@ -104,6 +108,7 @@ export class MarkerClusterComponent implements OnChanges, OnDestroy, AfterConten
});
this.supercluster = supercluster(options);
this.supercluster.load(this.data.features);
this.load.emit(this.supercluster);
}

ngOnChanges(changes: SimpleChanges) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/showcase/demo/demo.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { NgxCustomMarkerIconsComponent } from './examples/ngx-custom-marker-icon
import { NgxDragAMarkerComponent } from './examples/ngx-drag-a-marker.component';
import { NgxDragAPointComponent } from './examples/ngx-drag-a-point.component';
import { NgxGeoJSONLineComponent } from './examples/ngx-geojson-line.component';
import { ClusterPointComponent, NgxMarkerClusterComponent } from './examples/ngx-marker-cluster.component';
import { ClusterPopupComponent, NgxMarkerClusterComponent } from './examples/ngx-marker-cluster.component';
import { NgxScaleControlComponent } from './examples/ngx-scale-control.component';
import { PopupOnClickComponent } from './examples/popup-on-click.component';
import { PopupComponent } from './examples/popup.component';
Expand Down Expand Up @@ -144,7 +144,7 @@ export const DEMO_ROUTES: Routes = [
PopupOnClickComponent,
ZoomtoLinestringComponent,
NgxMarkerClusterComponent,
ClusterPointComponent,
ClusterPopupComponent,
Display3dBuildingsComponent
]
})
Expand Down
94 changes: 75 additions & 19 deletions src/app/showcase/demo/examples/ngx-marker-cluster.component.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
import { Component, OnInit, Input } from '@angular/core';
import { Component, Input, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core';
import { MatPaginator, PageEvent } from '@angular/material';
import { LngLatLike } from 'mapbox-gl';
import { Cluster, Supercluster } from 'supercluster';

@Component({
// tslint:disable-next-line:component-selector
selector: 'demo-cluster-point',
selector: 'demo-cluster-popup',
template: `
<div class="marker-cluster" (click)="alert()">
{{ feature.properties?.point_count }}
</div>
`
<mat-list>
<mat-list-item
*ngFor="let leaf of leaves"
>
{{ leaf.properties['Primary ID'] }}
</mat-list-item>
</mat-list>
<mat-paginator
[length]="count"
[pageSize]="5"
(page)="changePage($event)"
></mat-paginator>
`
})
export class ClusterPointComponent {
@Input() feature: GeoJSON.Feature<GeoJSON.Point>;
@Input() getLeavesFn: (limit?: number, offset?: number) => GeoJSON.Feature<GeoJSON.Point>[];
@Input() getClusterExpansionZoomFn: () => number;

alert() {
const clusters = this.getLeavesFn();
let message = 'childs: ';
message += clusters.map((cluster) => cluster.properties!['Primary ID']).join(', ');
message += ` getClusterExpansionZoom(): ${this.getClusterExpansionZoomFn()}`;
alert(message);
export class ClusterPopupComponent implements OnChanges {
@Input() clusterId: GeoJSON.Feature<GeoJSON.Point>;
@Input() supercluster: Supercluster;
@Input() count: number;

@ViewChild(MatPaginator) paginator: MatPaginator;

leaves: GeoJSON.Feature<GeoJSON.Point>[];

ngOnChanges(changes: SimpleChanges) {
this.changePage();
if (changes.count && !changes.count.isFirstChange()) {
this.paginator.firstPage();
}
}

changePage(pageEvent?: PageEvent) {
let offset = 0;
if (pageEvent) {
offset = pageEvent.pageIndex * 5;
}
// Typing issue in supercluster
this.leaves = (<any>this.supercluster.getLeaves)(this.clusterId, 5, offset);
}
}

Expand All @@ -36,6 +60,7 @@ export class ClusterPointComponent {
[data]="earthquakes"
[maxZoom]="14"
[radius]="50"
(load)="supercluster = $event"
>
<ng-template mglPoint let-feature>
<div
Expand All @@ -45,18 +70,49 @@ export class ClusterPointComponent {
{{ feature.properties['Primary ID'] }}
</div>
</ng-template>
<ng-template mglClusterPoint let-feature let-getLeavesFn="getLeavesFn" let-getClusterExpansionZoomFn="getClusterExpansionZoomFn">
<demo-cluster-point [feature]="feature" [getLeavesFn]="getLeavesFn" [getClusterExpansionZoomFn]="getClusterExpansionZoomFn"></demo-cluster-point>
<ng-template mglClusterPoint let-feature>
<div
class="marker-cluster"
(click)="selectCluster($event, feature)"
>
{{ feature.properties?.point_count }}
</div>
</ng-template>
</mgl-marker-cluster>
<mgl-popup
*ngIf="selectedCluster"
[lngLat]="selectedCluster.lngLat"
>
<demo-cluster-popup
[supercluster]="supercluster"
[clusterId]="selectedCluster.id"
[count]="selectedCluster.count"
></demo-cluster-popup>
</mgl-popup>
</mgl-map>
`,
styleUrls: ['./examples.css', './ngx-marker-cluster.component.css']
})
export class NgxMarkerClusterComponent implements OnInit {
earthquakes: object;
supercluster: Supercluster;
selectedCluster: {
lngLat: LngLatLike;
count: number;
id: number;
};

async ngOnInit() {
this.earthquakes = await import('./earthquakes.geo.json');
}

selectCluster(event: MouseEvent, feature: Cluster) {
event.stopPropagation(); // This is needed, otherwise the popup will close immediately
this.selectedCluster = {
// Change the ref, to trigger mgl-popup onChanges (when the user click on the same cluster)
lngLat: [ ...feature.geometry!.coordinates ],
count: feature.properties.point_count!,
id: feature.properties.cluster_id!
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ margin: 0;
'@angular/forms': '*',
'url': '*',
'querystring': '*',
'@types/mapbox-gl': '*'
'rxjs-compat': '*',
'@types/mapbox-gl': '*',
'@types/supercluster': '*'
}
};
await this.zone.runOutsideAngular(async () => {
Expand Down
4 changes: 3 additions & 1 deletion src/app/showcase/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
MatRadioModule,
MatSidenavModule,
MatSlideToggleModule,
MatToolbarModule
MatToolbarModule,
MatPaginatorModule
} from '@angular/material';
import { NgxMapboxGLModule } from '../lib';

Expand All @@ -31,6 +32,7 @@ const Modules = [
MatInputModule,
MatIconModule,
MatSidenavModule,
MatPaginatorModule,
FormsModule,
NgxMapboxGLModule
];
Expand Down
4 changes: 3 additions & 1 deletion src/assets/stackblitz-base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
MatProgressSpinnerModule,
MatRadioModule,
MatSlideToggleModule,
MatToolbarModule
MatToolbarModule,
MatPaginatorModule
} from '@angular/material';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
Expand All @@ -33,6 +34,7 @@ const demoArray = Object.values(demo);
MatCardModule,
MatSlideToggleModule,
MatProgressSpinnerModule,
MatPaginatorModule,
NgxMapboxGLModule.forRoot({
accessToken: 'pk.eyJ1Ijoid3lra3NzIiwiYSI6ImNqMjR6aTdmdzAwNHMzMnBvbjBucjlqNm8ifQ.6GjGpofWBVaIuSnhdXQb5w'
})
Expand Down

0 comments on commit a87bd6f

Please sign in to comment.