Skip to content

Commit 67af0c1

Browse files
authored
fix(google-maps): handle marker + mapOptions.styles conflict (#719)
1 parent 8ef0d39 commit 67af0c1

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

docs/content/scripts/google-maps/1.guides/2.map-styling.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ Google Maps supports two styling approaches: legacy JSON styles and cloud-based
88

99
Use the `mapOptions.styles` prop with a JSON style array. You can find pre-made styles on [Snazzy Maps](https://snazzymaps.com/).
1010

11-
Styles automatically apply to both the static map placeholder and the interactive map.
11+
Styles apply to both the static map placeholder and the interactive map.
12+
13+
::callout{color="amber"}
14+
JSON `styles` cannot combine with `ScriptGoogleMapsMarker`. `AdvancedMarkerElement` requires a `mapId`, and Google Maps treats `styles` and `mapId` as mutually exclusive. If the map contains markers, use [cloud-based map IDs](#cloud-based-map-ids) instead.
15+
::
1216

1317
```vue
1418
<script setup lang="ts">
@@ -63,7 +67,7 @@ Google's [Map Styling](https://developers.google.com/maps/documentation/cloud-cu
6367
```
6468

6569
::callout{color="amber"}
66-
JSON `styles` and `mapId` are mutually exclusive. When you provide both, the component ignores `mapId` and applies `styles`. Note that `AdvancedMarkerElement` requires a map ID to function; legacy `Marker` works without one.
70+
JSON `styles` and `mapId` are mutually exclusive. When both are set, Google Maps keeps `mapId` and ignores `styles`. Because `ScriptGoogleMapsMarker` uses `AdvancedMarkerElement` (which requires a `mapId`), cloud-based map IDs are the only way to style a map that contains markers.
6771
::
6872

6973
## Dark Mode / Color Mode

packages/script/src/runtime/components/GoogleMaps/ScriptGoogleMaps.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,11 @@ const { load, status, onLoaded } = useScriptGoogleMaps({
223223
})
224224
225225
const options = computed(() => {
226-
const mapId = props.mapOptions?.styles ? undefined : (currentMapId.value || 'map')
227-
// Precedence (defu merges left-to-right, leftmost wins):
228-
// 1. centerOverride: resolved query result, always wins for center
229-
// 2. mapOptions: preferred public API
230-
// 3. deprecated top-level: legacy fallback for center/zoom
231-
// 4. defaults: { zoom: 15 } when nothing else is set
226+
// JSON `styles` and `mapId` are mutually exclusive in Google Maps. When the user
227+
// sets `styles`, we drop `mapId` so styles apply. Otherwise fall back to a map ID
228+
// so `AdvancedMarkerElement` can function. The marker component warns if markers
229+
// are mounted against a styled (mapId-less) map.
230+
const mapId = props.mapOptions?.styles ? undefined : (currentMapId.value || 'DEMO_MAP_ID')
232231
return defu(
233232
{ center: centerOverride.value, mapId },
234233
props.mapOptions,

packages/script/src/runtime/components/GoogleMaps/ScriptGoogleMapsMarker.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ let gmpClickHandler: ((e: any) => void) | undefined
5757
const advancedMarkerElement = useGoogleMapsResource<google.maps.marker.AdvancedMarkerElement>({
5858
ready: () => !slots.content || !!markerContent.value,
5959
async create({ mapsApi, map }) {
60+
if (import.meta.dev && !map.get('mapId')) {
61+
console.warn('[nuxt-scripts] <ScriptGoogleMapsMarker> requires the parent <ScriptGoogleMaps> to have a `mapId` set, but none was found. This usually happens when `mapOptions.styles` (JSON styles) is set, since Google Maps treats `styles` and `mapId` as mutually exclusive. Use cloud-based map styling instead: https://scripts.nuxt.com/scripts/google-maps/guides/map-styling')
62+
}
6063
await mapsApi.importLibrary('marker')
6164
const marker = new mapsApi.marker.AdvancedMarkerElement({
6265
...props.options,

0 commit comments

Comments
 (0)