Skip to content

Commit 45fd298

Browse files
committedMar 25, 2025·
fix(GoogleMaps): dedupe markers based on position
Fixes #436
1 parent 8c75418 commit 45fd298

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed
 

Diff for: ‎src/runtime/components/ScriptGoogleMaps.vue

+23-12
Original file line numberDiff line numberDiff line change
@@ -166,27 +166,38 @@ function resetMapMarkerMap(_marker: google.maps.marker.AdvancedMarkerElement | P
166166
})
167167
}
168168
169+
function normalizeAdvancedMapMarkerOptions(_options?: google.maps.marker.AdvancedMarkerElementOptions | `${string},${string}`) {
170+
const opts = typeof _options === 'string'
171+
? {
172+
position: {
173+
lat: Number.parseFloat(_options.split(',')[0] || '0'),
174+
lng: Number.parseFloat(_options.split(',')[1] || '0'),
175+
},
176+
}
177+
: _options
178+
if (!opts.position) {
179+
// set default
180+
opts.position = {
181+
lat: 0,
182+
lng: 0,
183+
}
184+
}
185+
}
186+
169187
async function createAdvancedMapMarker(_options?: google.maps.marker.AdvancedMarkerElementOptions | `${string},${string}`) {
170188
if (!_options)
171189
return
172-
const key = hash(_options)
190+
const normalizedOptions = normalizeAdvancedMapMarkerOptions(_options)
191+
const key = hash({ position: normalizedOptions.position })
173192
if (mapMarkers.value.has(key))
174193
return mapMarkers.value.get(key)
175194
// eslint-disable-next-line no-async-promise-executor
176195
const p = new Promise<google.maps.marker.AdvancedMarkerElement>(async (resolve) => {
177196
const lib = await importLibrary('marker')
178-
const options = typeof _options === 'string'
179-
? {
180-
position: {
181-
lat: Number.parseFloat(_options.split(',')[0] || '0'),
182-
lng: Number.parseFloat(_options.split(',')[1] || '0'),
183-
},
184-
}
185-
: _options
186-
const mapMarkerOptions = defu(toRaw(options), {
197+
const mapMarkerOptions = defu(toRaw(normalizedOptions), {
187198
map: toRaw(map.value!),
188199
// @ts-expect-error unified API for maps and markers
189-
position: options.location,
200+
position: normalizedOptions.location,
190201
})
191202
resolve(new lib.AdvancedMarkerElement(mapMarkerOptions))
192203
})
@@ -284,7 +295,7 @@ onMounted(() => {
284295
}
285296
// mapMarkers is a map where we hash the next array entry as the map key
286297
// we need to do a diff to see what we remove or add
287-
const nextMap = new Map((props.markers || []).map(m => [hash(m), m]))
298+
const nextMap = new Map((props.markers || []).map(m => [hash({ position: normalizeAdvancedMapMarkerOptions(m).position }), m]))
288299
// compare idsToMatch in nextMap, if we're missing an id, we need to remove it
289300
const toRemove = new Set([
290301
...mapMarkers.value.keys(),

0 commit comments

Comments
 (0)
Please sign in to comment.