-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 커스텀 마커 클러스터러, 커스텀 클러스터 알고리즘 #222
- Loading branch information
Showing
2 changed files
with
121 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,62 @@ | ||
import { | ||
AlgorithmInput, | ||
AlgorithmOutput, | ||
getPaddedViewport, | ||
MarkerUtils, | ||
SuperClusterViewportAlgorithm, | ||
SuperClusterViewportOptions, | ||
SuperClusterViewportState, | ||
} from '@googlemaps/markerclusterer'; | ||
import equal from 'fast-deep-equal'; | ||
|
||
export class CustomSuperClusterAlgorithm extends SuperClusterViewportAlgorithm { | ||
constructor({ ...options }: SuperClusterViewportOptions) { | ||
super(options); | ||
this.clusters = []; | ||
} | ||
|
||
public calculate(input: AlgorithmInput): AlgorithmOutput { | ||
const state: SuperClusterViewportState = { | ||
zoom: Math.round(input.map.getZoom()!), | ||
view: getPaddedViewport( | ||
input.map.getBounds()!, | ||
input.mapCanvasProjection, | ||
this.viewportPadding, | ||
), | ||
}; | ||
|
||
let changed = !equal(this.state, state); | ||
if (!equal(input.markers, this.markers)) { | ||
// TODO use proxy to avoid copy? | ||
this.markers = [...input.markers]; | ||
|
||
const points = this.markers.map((marker) => { | ||
const position = MarkerUtils.getPosition(marker); | ||
const coordinates = [position.lng(), position.lat()]; | ||
return { | ||
type: 'Feature' as const, | ||
geometry: { | ||
type: 'Point' as const, | ||
coordinates, | ||
}, | ||
properties: { marker }, | ||
}; | ||
}); | ||
this.superCluster.load(points); | ||
} | ||
|
||
const newClusters = this.cluster(input); | ||
console.log(newClusters.length, this.clusters.length); | ||
//this.clusters.length !== newClusters.length | ||
//!equal(this.clusters, newClusters) | ||
if (this.clusters.length !== newClusters.length) { | ||
this.clusters = newClusters; | ||
} else { | ||
changed = false; | ||
} | ||
|
||
this.state = state; | ||
|
||
return { clusters: this.clusters, changed }; | ||
} | ||
} |