Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ and `offset` is the amount of points to skip (for pagination).

Returns the zoom on which the cluster expands into several children (useful for "click to zoom" feature) given the cluster's `cluster_id`.

#### `getPointUnclusterZoom(point)`

Returns the zoom on which a point appears unclustered. The point must be provided as array with `[Lng, Lat]`.

## Options

| Option | Default | Description |
Expand Down
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ export default class Supercluster {
return expansionZoom;
}

getPointUnclusterZoom(point) {
const [lng, lat] = point;
const pointX = fround(lngX(lng));
const pointY = fround(latY(lat));

let expansionZoom = this.options.minZoom;
while (expansionZoom < this.options.maxZoom) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since points are more likely to get clustered on higher zooms, it would likely be faster to iterate starting from maxZoom and up until there are no points in the location or are all clusters.

const tree = this.trees[expansionZoom];

const pointIdxs = tree.within(pointX, pointY, 0);

const unclustered = pointIdxs.some(
idx => tree.points[idx].parentId !== -1
);
if (unclustered) return expansionZoom;

expansionZoom++;
}

return expansionZoom;
}

_appendLeaves(result, clusterId, limit, offset, skipped) {
const children = this.getChildren(clusterId);

Expand Down