Skip to content

Optimization render of cluster markers #321

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tools]
node = "18"
pnpm = "8"
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
"dependencies": {
"@googlemaps/js-api-loader": "^1.16.2",
"@googlemaps/markerclusterer": "^2.4.0",
"debounce": "^2.2.0",
"fast-deep-equal": "^3.1.3"
},
"devDependencies": {
"pnpm": "^8.7.6",
"@types/google.maps": "^3.58.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
Expand All @@ -67,6 +67,7 @@
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-vue": "^7.20.0",
"pnpm": "^8.7.6",
"prettier": "^2.8.8",
"rimraf": "^5.0.1",
"standard-version": "^9.5.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/components/AdvancedMarker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
watch,
Comment,
} from "vue";
import { markerSymbol, apiSymbol, mapSymbol, markerClusterSymbol } from "../shared/index";
import { markerSymbol, apiSymbol, mapSymbol, markerClusterSymbol, markerClusterMethodsSymbol } from "../shared/index";
import equal from "fast-deep-equal";

const markerEvents = ["click", "drag", "dragend", "dragstart", "gmp-click"];
Expand Down Expand Up @@ -50,6 +50,7 @@ export default defineComponent({
const map = inject(mapSymbol, ref());
const api = inject(apiSymbol, ref());
const markerCluster = inject(markerClusterSymbol, ref());
const markerClusterMethods = inject(markerClusterMethodsSymbol, undefined);

const isMarkerInCluster = computed(
() => !!(markerCluster.value && api.value && marker.value instanceof google.maps.marker.AdvancedMarkerElement)
Expand Down Expand Up @@ -78,8 +79,8 @@ export default defineComponent({
});

if (isMarkerInCluster.value) {
markerCluster.value?.removeMarker(marker.value);
markerCluster.value?.addMarker(marker.value);
markerClusterMethods?.removeMarker(marker.value);
markerClusterMethods?.addMarker(marker.value);
}
} else {
if (hasSlotContent.value) {
Expand All @@ -91,7 +92,7 @@ export default defineComponent({
marker.value = markRaw(new AdvancedMarkerElement(options.value));

if (isMarkerInCluster.value) {
markerCluster.value?.addMarker(marker.value);
markerClusterMethods?.addMarker(marker.value);
} else {
marker.value.map = map.value;
}
Expand All @@ -111,7 +112,7 @@ export default defineComponent({
api.value?.event.clearInstanceListeners(marker.value);

if (isMarkerInCluster.value) {
markerCluster.value?.removeMarker(marker.value);
markerClusterMethods?.removeMarker(marker.value);
} else {
marker.value.map = null;
}
Expand Down
29 changes: 28 additions & 1 deletion src/components/MarkerCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
MarkerClustererEvents,
SuperClusterViewportAlgorithm,
} from "@googlemaps/markerclusterer";
import { mapSymbol, apiSymbol, markerClusterSymbol } from "../shared/index";
import { mapSymbol, apiSymbol, markerClusterSymbol, markerClusterMethodsSymbol } from "../shared/index";
import debounce from 'debounce';

const markerClusterEvents = Object.values(MarkerClustererEvents);

Expand All @@ -16,14 +17,39 @@ export default defineComponent({
type: Object as PropType<MarkerClustererOptions>,
default: () => ({}),
},
renderDebounceDelay: {
type: Number,
default: 10,
}
},
emits: markerClusterEvents,
setup(props, { emit, expose, slots }) {
const markerCluster = ref<MarkerClusterer>();
const map = inject(mapSymbol, ref());
const api = inject(apiSymbol, ref());

const debouncedRender = debounce(() => {
if (markerCluster.value) {
markerCluster.value?.render();
}
}, props.renderDebounceDelay);

const addMarker = (marker: google.maps.Marker | google.maps.marker.AdvancedMarkerElement) => {
if (markerCluster.value) {
markerCluster.value?.addMarker(marker, true);
debouncedRender();
}
};

const removeMarker = (marker: google.maps.Marker | google.maps.marker.AdvancedMarkerElement) => {
if (markerCluster.value) {
markerCluster.value?.removeMarker(marker, true);
debouncedRender();
}
};

provide(markerClusterSymbol, markerCluster);
provide(markerClusterMethodsSymbol, { addMarker, removeMarker });

watch(
map,
Expand Down Expand Up @@ -55,6 +81,7 @@ export default defineComponent({
markerCluster.value.clearMarkers();
markerCluster.value.setMap(null);
}
debouncedRender.clear()
});

expose({ markerCluster });
Expand Down
11 changes: 6 additions & 5 deletions src/composables/useSetupMapComponent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { watch, ref, Ref, inject, onBeforeUnmount, computed, markRaw } from "vue";
import equal from "fast-deep-equal";
import { apiSymbol, mapSymbol, markerClusterSymbol, customMarkerClassSymbol } from "../shared/index";
import { apiSymbol, mapSymbol, markerClusterSymbol, customMarkerClassSymbol, markerClusterMethodsSymbol } from "../shared/index";

type ICtorKey = "Marker" | "Polyline" | "Polygon" | "Rectangle" | "Circle" | typeof customMarkerClassSymbol;

Expand Down Expand Up @@ -50,6 +50,7 @@ export const useSetupMapComponent = <T extends ICtorKey>(
const map = inject(mapSymbol, ref());
const api = inject(apiSymbol, ref());
const markerCluster = inject(markerClusterSymbol, ref());
const markerClusterMethods = inject(markerClusterMethodsSymbol, undefined);

const isMarkerInCluster = computed(
() =>
Expand All @@ -72,8 +73,8 @@ export const useSetupMapComponent = <T extends ICtorKey>(
component.value.setOptions(options.value as any);

if (isMarkerInCluster.value) {
markerCluster.value?.removeMarker(component.value as google.maps.Marker);
markerCluster.value?.addMarker(component.value as google.maps.Marker);
markerClusterMethods?.removeMarker(component.value as google.maps.Marker);
markerClusterMethods?.addMarker(component.value as google.maps.Marker);
}
} else {
if (isMarkerCtorKey(ctorKey)) {
Expand All @@ -94,7 +95,7 @@ export const useSetupMapComponent = <T extends ICtorKey>(
}

if (isMarkerInCluster.value) {
markerCluster.value?.addMarker(component.value as google.maps.Marker);
markerClusterMethods?.addMarker(component.value as google.maps.Marker);
} else {
component.value.setMap(map.value);
}
Expand All @@ -114,7 +115,7 @@ export const useSetupMapComponent = <T extends ICtorKey>(
api.value?.event.clearInstanceListeners(component.value);

if (isMarkerInCluster.value) {
markerCluster.value?.removeMarker(component.value as google.maps.Marker);
markerClusterMethods?.removeMarker(component.value as google.maps.Marker);
} else {
component.value.setMap(null);
}
Expand Down
5 changes: 5 additions & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export const markerSymbol: InjectionKey<
Ref<google.maps.Marker | google.maps.marker.AdvancedMarkerElement | undefined>
> = Symbol("marker");
export const markerClusterSymbol: InjectionKey<Ref<MarkerClusterer | undefined>> = Symbol("markerCluster");
export const markerClusterMethodsSymbol: InjectionKey<{
addMarker: (marker: google.maps.Marker | google.maps.marker.AdvancedMarkerElement) => void;
removeMarker: (marker: google.maps.Marker | google.maps.marker.AdvancedMarkerElement) => void;
} | undefined> = Symbol("markerClusterMethods");

export const customMarkerClassSymbol = Symbol("CustomMarker") as unknown as "CustomMarker";
/**
* Utilitary flag for components that need to know the map
Expand Down