Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: advanced marker position can be null #903

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions src/algorithms/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,18 @@ export class GridAlgorithm extends AbstractViewportAlgorithm {
): void {
let maxDistance = this.maxDistance; // Some large number
let cluster: Cluster = null;
const position = MarkerUtils.getPosition(marker);

if (position === null) {
// Marker has no position, which makes it invisible and not part of the cluster
return;
}

for (let i = 0; i < this.clusters.length; i++) {
const candidate = this.clusters[i];
const distance = distanceBetweenPoints(
candidate.bounds.getCenter().toJSON(),
MarkerUtils.getPosition(marker).toJSON()
position.toJSON()
);

if (distance < maxDistance) {
Expand All @@ -135,7 +141,7 @@ export class GridAlgorithm extends AbstractViewportAlgorithm {
cluster.bounds,
projection,
this.gridSize
).contains(MarkerUtils.getPosition(marker))
).contains(position)
) {
cluster.push(marker);
} else {
Expand Down
6 changes: 5 additions & 1 deletion src/algorithms/supercluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export class SuperClusterAlgorithm extends AbstractAlgorithm {

const points = this.markers.map((marker) => {
const position = MarkerUtils.getPosition(marker);
if (!position) {
return null;
}

const coordinates = [position.lng(), position.lat()];
return {
type: "Feature" as const,
Expand All @@ -69,7 +73,7 @@ export class SuperClusterAlgorithm extends AbstractAlgorithm {
properties: { marker },
};
});
this.superCluster.load(points);
this.superCluster.load(points.filter((p) => p != null));
}

if (!changed) {
Expand Down
6 changes: 5 additions & 1 deletion src/algorithms/superviewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export class SuperClusterViewportAlgorithm extends AbstractViewportAlgorithm {

const points = this.markers.map((marker) => {
const position = MarkerUtils.getPosition(marker);
if (!position) {
return null;
}

const coordinates = [position.lng(), position.lat()];
return {
type: "Feature" as const,
Expand All @@ -95,7 +99,7 @@ export class SuperClusterViewportAlgorithm extends AbstractViewportAlgorithm {
properties: { marker },
};
});
this.superCluster.load(points);
this.superCluster.load(points.filter((p) => p != null));
}

if (changed) {
Expand Down
7 changes: 4 additions & 3 deletions src/algorithms/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export const filterMarkersToPaddedViewport = (
mapCanvasProjection,
viewportPaddingPixels
);
return markers.filter((marker) =>
extendedMapBounds.contains(MarkerUtils.getPosition(marker))
);
return markers.filter((marker) => {
const position = MarkerUtils.getPosition(marker);
return position ? extendedMapBounds.contains(position) : false;
});
};

/**
Expand Down
5 changes: 4 additions & 1 deletion src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ export class Cluster {

const bounds = new google.maps.LatLngBounds(this._position, this._position);
for (const marker of this.markers) {
bounds.extend(MarkerUtils.getPosition(marker));
const position = MarkerUtils.getPosition(marker);
if (position) {
bounds.extend(position);
}
}
return bounds;
}
Expand Down
17 changes: 9 additions & 8 deletions src/marker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class MarkerUtils {
}
}

public static getPosition(marker: Marker): google.maps.LatLng {
public static getPosition(marker: Marker): google.maps.LatLng | null {
// SuperClusterAlgorithm.calculate expects a LatLng instance so we fake it for Adv Markers
if (this.isAdvancedMarker(marker)) {
if (marker.position) {
Expand All @@ -65,21 +65,22 @@ export class MarkerUtils {
);
}
}
return new google.maps.LatLng(null);
return null;
}
return marker.getPosition();
}

public static getVisible(marker: Marker) {
if (this.isAdvancedMarker(marker)) {
/**
* Always return true for Advanced Markers because the clusterer
* uses getVisible as a way to count legacy markers not as an actual
* indicator of visibility for some reason. Even when markers are hidden
* Marker.getVisible returns `true` and this is used to set the marker count
* on the cluster. See the behavior of Cluster.count
* As per google.maps.marker.AdvancedMarkerElement documentation:
* An AdvancedMarkerElement may be constructed without a position,
* but will not be displayed until its position is provided.
*
* The same goes for the map property. But in case of clustering this
* is always set to null as we don't want to show the marker on the map.
*/
return true;
return marker.position !== null;
}
return marker.getVisible();
}
Expand Down