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: enables position retrieval for advanced markers with a lat or lng of 0 #927

Draft
wants to merge 5 commits 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
78 changes: 67 additions & 11 deletions src/marker-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ const markerClasses = [
google.maps.marker.AdvancedMarkerElement,
];

const latLngLiteralTestScenarios = [
{ lat: 1, lng: 1 },
{ lat: 0, lng: 0 },
{ lat: 0, lng: 1 },
{ lat: 1, lng: 0 },
];

const latLngTestScenarios = [
new google.maps.LatLng(1, 1),
new google.maps.LatLng(0, 0),
new google.maps.LatLng(0, 1),
new google.maps.LatLng(1, 0),
];

describe.each(markerClasses)(
"MarkerUtils works with legacy and Advanced Markers",
(markerClass) => {
Expand Down Expand Up @@ -53,21 +67,63 @@ describe.each(markerClasses)(
expect((marker as google.maps.Marker).setMap).toHaveBeenCalled;
});

test("gets the marker position and returns a LatLng", () => {
// test markers created with LatLng and LatLngLiteral
[new google.maps.LatLng(1, 1), { lat: 1, lng: 1 }].forEach((position) => {
const marker = new markerClass({ position: position });
if (markerClass === google.maps.marker.AdvancedMarkerElement) {
(marker as google.maps.marker.AdvancedMarkerElement).position =
position;
test("gets the marker position from an AdvancedMarkerElement with a LatLng position", () => {
latLngTestScenarios.forEach((position: google.maps.LatLng) => {
const marker = new google.maps.marker.AdvancedMarkerElement();

marker.position = position;

const markerPosition = MarkerUtils.getPosition(marker);

expect(markerPosition).toBeInstanceOf(google.maps.LatLng);
expect(markerPosition.lat()).toEqual(position.lat());
expect(markerPosition.lng()).toEqual(position.lng());
});
});

test("gets the marker position from an AdvancedMarkerElement with a LatLngLiteral position", () => {
latLngLiteralTestScenarios.forEach(
(position: google.maps.LatLngLiteral) => {
const marker = new google.maps.marker.AdvancedMarkerElement();

marker.position = position;

const markerPosition = MarkerUtils.getPosition(marker);

expect(markerPosition).toBeInstanceOf(google.maps.LatLng);
expect(markerPosition.lat()).toEqual(position.lat);
expect(markerPosition.lng()).toEqual(position.lng);
}
expect(MarkerUtils.getPosition(marker)).toBeInstanceOf(
google.maps.LatLng
);
);
});

test("gets the marker position from a Marker with a LatLng position", () => {
latLngTestScenarios.forEach((position: google.maps.LatLng) => {
const marker = new google.maps.Marker({ position: position });

const markerPosition = MarkerUtils.getPosition(marker);

expect(markerPosition).toBeInstanceOf(google.maps.LatLng);
expect(markerPosition.lat()).toEqual(position.lat());
expect(markerPosition.lng()).toEqual(position.lng());
});
});

test("", () => {
test("gets the marker position from a Marker with a LatLngLiteral position", () => {
latLngLiteralTestScenarios.forEach(
(position: google.maps.LatLngLiteral) => {
const marker = new google.maps.Marker({ position: position });

const markerPosition = MarkerUtils.getPosition(marker);

expect(markerPosition).toBeInstanceOf(google.maps.LatLng);
expect(markerPosition.lat()).toEqual(position.lat);
expect(markerPosition.lng()).toEqual(position.lng);
}
);
});

test("detects the visibility of a marker", () => {
const marker = new markerClass();
expect(MarkerUtils.getVisible(marker)).toBeTruthy;
});
Expand Down
24 changes: 23 additions & 1 deletion src/marker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,41 @@ export class MarkerUtils {
// SuperClusterAlgorithm.calculate expects a LatLng instance so we fake it for Adv Markers
if (this.isAdvancedMarker(marker)) {
if (marker.position) {
/*
TODO: This check doesn't work when a `loader` is used to create the position object.
The type check sees `marker.position` as a generic `object`, but I'm not sure how to
fix this. Example `LatLng` object creation to reproduce:
```ts
import {Loader} from '@googlemaps/js-api-loader';

const loader = new Loader(...);
const {LatLng} = await loader.importLibrary('core');
const latLng = new LatLng({lat: latitude, lng: longitude});

const newAdvancedMarker = new AdvancedMarkerElement({
position: latLng,
...
});
```
*/
if (marker.position instanceof google.maps.LatLng) {
return marker.position;
}
// since we can't cast to LatLngLiteral for reasons =(
if (marker.position.lat && marker.position.lng) {
if (
typeof marker.position.lat === "number" &&
typeof marker.position.lng === "number"
) {
return new google.maps.LatLng(
marker.position.lat,
marker.position.lng
);
}
}

return new google.maps.LatLng(null);
}

return marker.getPosition();
}

Expand Down