Created
April 25, 2022 02:37
-
-
Save alfiankan/a3ecf8f435b7472c19f9ca1d1fe88435 to your computer and use it in GitHub Desktop.
clustering use cae supercluster
This file contains hidden or 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
import { Locations } from '../repositories/Locations' | |
import Supercluster = require('supercluster') | |
class LocationCluster { | |
private locationRepository: Locations | |
constructor(locationsRepository: Locations) { | |
this.locationRepository = locationsRepository | |
} | |
async getClusteredPoint(west: number, south: number, east: number, north: number, zoom: number): Promise<Array<Supercluster.ClusterFeature<any>>> { | |
const [locations] = await Promise.all([ | |
this.locationRepository.getPointsInsideBoundingBox( | |
String(west), | |
String(south), | |
String(east), | |
String(north), | |
) | |
]) | |
// converting to .geojson features array | |
let geojson = new Array<any>() | |
locations.rows.map(point => { | |
geojson.push({ | |
"type": "Feature", | |
"geometry": { | |
"type": "Point", | |
"coordinates": [point.x, point.y] | |
}, | |
"properties": { | |
"name": point.id | |
} | |
}) | |
}) | |
const clustersIndexes = new Supercluster({ | |
log: true, | |
radius: 80, | |
maxZoom: 17 | |
}); | |
clustersIndexes.load(geojson) | |
return clustersIndexes.getClusters([west, south, east, north], zoom) | |
} | |
} | |
export { LocationCluster } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment