Skip to content

Commit c1fdb3f

Browse files
feat(AdvancedMarker): support slot custom content
* feat(AdvancedMarker): Support slot custom content * chore: clean * style: format advanced marker code * docs: update advanced marker docs --------- Co-authored-by: Husam Elbashir <[email protected]>
1 parent 29720fb commit c1fdb3f

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

docs/components/advanced-marker.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ You can pass a [AdvancedMarkerElementOptions](https://developers.google.com/maps
1818

1919
You can also pass a [PinElementOptions interface](https://developers.google.com/maps/documentation/javascript/reference/advanced-markers#PinElementOptions) object to custumize pin used by the marker.
2020

21+
Additionally, `AdvancedMarker` supports default slot content, allowing you to use custom HTML or Vue components inside the marker.
22+
2123
```vue
2224
<script setup>
2325
import { GoogleMap, AdvancedMarker } from 'vue3-google-map'
@@ -36,6 +38,11 @@ const pinOptions = { background: '#FBBC04' }
3638
:zoom="15"
3739
>
3840
<AdvancedMarker :options="markerOptions" :pin-options="pinOptions"/>
41+
<AdvancedMarker :options="markerOptions">
42+
<div style="background: white; color: black; padding: 5px; border-radius: 5px">
43+
Custom Content
44+
</div>
45+
</AdvancedMarker>
3946
</GoogleMap>
4047
</template>
4148
```
@@ -49,6 +56,11 @@ const pinOptions = { background: '#FBBC04' }
4956
:zoom="15"
5057
>
5158
<AdvancedMarker :options="{ position: { lat: 40.689247, lng: -74.044502 } }" :pin-options="{ background: '#FBBC04' }" />
59+
<AdvancedMarker :options="{ position: { lat: 40.689247, lng: -74.044502 } }">
60+
<div style="background: white; color: black; padding: 5px; border-radius: 5px;">
61+
Custom Content
62+
</div>
63+
</AdvancedMarker>
5264
</GoogleMap>
5365
</ClientOnly>
5466

playground/App.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<script setup>
2-
import { GoogleMap, AdvancedMarker } from '../src'
2+
import { GoogleMap, AdvancedMarker } from "../src";
33
4-
const center = { lat: 40.689247, lng: -74.044502 }
5-
const markerOptions = { position: center, title: 'LADY LIBERTY' }
6-
const pinOptions = { background: '#FBBC04' }
4+
const center = { lat: 40.689247, lng: -74.044502 };
5+
const markerOptions = { position: center, title: "LADY LIBERTY" };
76
</script>
87

98
<template>
109
<GoogleMap mapId="DEMO_MAP_ID" style="width: 100%; height: 500px" :center="center" :zoom="15">
11-
<AdvancedMarker :options="markerOptions" :pin-options="pinOptions" />
10+
<AdvancedMarker :options="markerOptions">
11+
<div style="border: 1px solid red">advanced marker slot</div>
12+
</AdvancedMarker>
1213
</GoogleMap>
1314
</template>

src/components/AdvancedMarker.ts renamed to src/components/AdvancedMarker.vue

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
import { defineComponent, PropType, toRef, provide, computed, inject, markRaw, onBeforeUnmount, ref, watch } from "vue";
1+
<template>
2+
<div v-if="hasSlotContent" class="advanced-marker-wrapper">
3+
<div ref="markerRef" v-bind="$attrs">
4+
<slot />
5+
</div>
6+
</div>
7+
</template>
8+
9+
<script lang="ts">
10+
import {
11+
defineComponent,
12+
PropType,
13+
toRef,
14+
provide,
15+
computed,
16+
inject,
17+
markRaw,
18+
onBeforeUnmount,
19+
ref,
20+
watch,
21+
Comment,
22+
} from "vue";
223
import { markerSymbol, apiSymbol, mapSymbol, markerClusterSymbol } from "../shared/index";
324
import equal from "fast-deep-equal";
425
@@ -18,6 +39,9 @@ export default defineComponent({
1839
},
1940
emits: markerEvents,
2041
setup(props, { emit, expose, slots }) {
42+
const markerRef = ref<HTMLElement>();
43+
const hasSlotContent = computed(() => slots.default?.().some((vnode) => vnode.type !== Comment));
44+
2145
const options = toRef(props, "options");
2246
const pinOptions = toRef(props, "pinOptions");
2347
@@ -45,7 +69,11 @@ export default defineComponent({
4569
const { map: _, content, ...otherOptions } = options.value;
4670
4771
Object.assign(marker.value, {
48-
content: pinOptions.value ? new PinElement(pinOptions.value).element : content,
72+
content: hasSlotContent.value
73+
? markerRef.value
74+
: pinOptions.value
75+
? new PinElement(pinOptions.value).element
76+
: content,
4977
...otherOptions,
5078
});
5179
@@ -54,7 +82,9 @@ export default defineComponent({
5482
markerCluster.value?.addMarker(marker.value);
5583
}
5684
} else {
57-
if (pinOptions.value) {
85+
if (hasSlotContent.value) {
86+
options.value.content = markerRef.value;
87+
} else if (pinOptions.value) {
5888
options.value.content = new PinElement(pinOptions.value).element;
5989
}
6090
@@ -92,6 +122,17 @@ export default defineComponent({
92122
93123
expose({ marker });
94124
95-
return () => slots.default?.();
125+
return { hasSlotContent, markerRef };
96126
},
97127
});
128+
</script>
129+
130+
<style>
131+
.advanced-marker-wrapper {
132+
display: none;
133+
}
134+
135+
.mapdiv .advanced-marker-wrapper {
136+
display: inline-block;
137+
}
138+
</style>

src/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { default as GoogleMap } from "./GoogleMap.vue";
2-
export { default as AdvancedMarker } from "./AdvancedMarker";
2+
export { default as AdvancedMarker } from "./AdvancedMarker.vue";
33
export { default as Marker } from "./Marker";
44
export { default as Polyline } from "./Polyline";
55
export { default as Polygon } from "./Polygon";

0 commit comments

Comments
 (0)