Skip to content

Commit 32a38e3

Browse files
authored
fix(examples): use advanced markers when available (#668)
1 parent 60c3de3 commit 32a38e3

File tree

7 files changed

+41
-45
lines changed

7 files changed

+41
-45
lines changed

examples/algorithms.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
NoopAlgorithm,
2222
SuperClusterAlgorithm,
2323
} from "../src";
24-
import { MAP_ID, getLoaderOptions, sync } from "./config";
24+
import { MAP_ID, createMarker, getLoaderOptions, sync } from "./config";
2525

2626
import { Loader } from "@googlemaps/js-api-loader";
2727
import trees from "./trees.json";
@@ -63,15 +63,8 @@ new Loader(getLoaderOptions()).load().then(() => {
6363

6464
map.controls[google.maps.ControlPosition.LEFT_TOP].push(textElement);
6565

66-
const markers = trees.map(
67-
({ geometry }) =>
68-
new google.maps.Marker({
69-
position: {
70-
lat: geometry.coordinates[1],
71-
lng: geometry.coordinates[0],
72-
},
73-
map,
74-
})
66+
const markers = trees.map(({ geometry }) =>
67+
createMarker(map, geometry.coordinates[1], geometry.coordinates[0])
7568
);
7669

7770
new MarkerClusterer({

examples/config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { LoaderOptions } from "@googlemaps/js-api-loader";
18+
import { MarkerUtils } from "../src/marker-utils";
1819

1920
export const MAP_ID = "DEMO_MAP_ID";
2021

@@ -55,3 +56,20 @@ export const sync = (...maps: google.maps.Map[]): void => {
5556
});
5657
});
5758
};
59+
60+
// Creates a marker.
61+
//
62+
// Prefers advanced markers when they are available.
63+
export function createMarker(map: google.maps.Map, lat: number, lng: number) {
64+
if (MarkerUtils.isAdvancedMarkerAvailable(map)) {
65+
return new google.maps.marker.AdvancedMarkerElement({
66+
map,
67+
position: { lat, lng },
68+
});
69+
}
70+
71+
return new google.maps.Marker({
72+
position: { lat, lng },
73+
map,
74+
});
75+
}

examples/defaults.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { MAP_ID, getLoaderOptions } from "./config";
17+
import { MAP_ID, createMarker, getLoaderOptions } from "./config";
1818
import { Loader } from "@googlemaps/js-api-loader";
1919
import { MarkerClusterer } from "../src";
2020
import trees from "./trees.json";
@@ -30,15 +30,8 @@ new Loader(getLoaderOptions()).load().then(() => {
3030

3131
const map = new google.maps.Map(element, mapOptions);
3232

33-
const markers = trees.map(
34-
({ geometry }) =>
35-
new google.maps.Marker({
36-
position: {
37-
lat: geometry.coordinates[1],
38-
lng: geometry.coordinates[0],
39-
},
40-
map,
41-
})
33+
const markers = trees.map(({ geometry }) =>
34+
createMarker(map, geometry.coordinates[1], geometry.coordinates[0])
4235
);
4336

4437
const markerCluster = new MarkerClusterer({

examples/renderers.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
MarkerClusterer,
2222
Renderer,
2323
} from "../src";
24-
import { MAP_ID, getLoaderOptions, sync } from "./config";
24+
import { MAP_ID, createMarker, getLoaderOptions, sync } from "./config";
2525

2626
import { Loader } from "@googlemaps/js-api-loader";
2727
import { interpolateRgb } from "d3-interpolate";
@@ -108,15 +108,8 @@ new Loader(getLoaderOptions()).load().then(() => {
108108

109109
map.controls[google.maps.ControlPosition.LEFT_TOP].push(textElement);
110110

111-
const markers = trees.map(
112-
({ geometry }) =>
113-
new google.maps.Marker({
114-
position: {
115-
lat: geometry.coordinates[1],
116-
lng: geometry.coordinates[0],
117-
},
118-
map,
119-
})
111+
const markers = trees.map(({ geometry }) =>
112+
createMarker(map, geometry.coordinates[1], geometry.coordinates[0])
120113
);
121114

122115
new MarkerClusterer({

examples/updates.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { MAP_ID, getLoaderOptions } from "./config";
17+
import { MAP_ID, createMarker, getLoaderOptions } from "./config";
1818
import { Loader } from "@googlemaps/js-api-loader";
1919
import { MarkerClusterer } from "../src";
2020
import trees from "./trees.json";
@@ -30,14 +30,8 @@ new Loader(getLoaderOptions()).load().then(async () => {
3030

3131
const map = new google.maps.Map(element, mapOptions);
3232

33-
const markers = trees.map(
34-
({ geometry }) =>
35-
new google.maps.Marker({
36-
position: {
37-
lat: geometry.coordinates[1],
38-
lng: geometry.coordinates[0],
39-
},
40-
})
33+
const markers = trees.map(({ geometry }) =>
34+
createMarker(map, geometry.coordinates[1], geometry.coordinates[0])
4135
);
4236

4337
const markerCluster = new MarkerClusterer({

src/marker-utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export type Marker =
2424
| google.maps.marker.AdvancedMarkerElement;
2525

2626
export class MarkerUtils {
27+
public static isAdvancedMarkerAvailable(map: google.maps.Map): boolean {
28+
return (
29+
google.maps.marker &&
30+
map.getMapCapabilities().isAdvancedMarkersAvailable === true
31+
);
32+
}
33+
2734
public static isAdvancedMarker(
2835
marker: Marker
2936
): marker is google.maps.marker.AdvancedMarkerElement {

src/renderer.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { Cluster } from "./cluster";
18-
import { Marker } from "./marker-utils";
18+
import { Marker, MarkerUtils } from "./marker-utils";
1919

2020
/**
2121
* Provides statistics on all clusters in the current render cycle for use in {@link Renderer.render}.
@@ -51,7 +51,7 @@ export class ClusterStats {
5151

5252
export interface Renderer {
5353
/**
54-
* Turn a {@link Cluster} into a `google.maps.Marker`.
54+
* Turn a {@link Cluster} into a `Marker`.
5555
*
5656
* Below is a simple example to create a marker with the number of markers in the cluster as a label.
5757
*
@@ -125,10 +125,8 @@ export class DefaultRenderer implements Renderer {
125125
// adjust zIndex to be above other markers
126126
zIndex: number = Number(google.maps.Marker.MAX_ZINDEX) + count;
127127

128-
if (
129-
google.maps.marker &&
130-
map.getMapCapabilities().isAdvancedMarkersAvailable
131-
) {
128+
if (MarkerUtils.isAdvancedMarkerAvailable(map)) {
129+
// create cluster SVG element
132130
const div = document.createElement("div");
133131
div.innerHTML = svg;
134132
const svgEl = div.firstElementChild;

0 commit comments

Comments
 (0)