-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add AdvancedMarker component * Add doc about advanced marker component * Update component on options change * Rename avanced-marker.md to advanced-marker.md * fix: minor fixes * docs: update readme --------- Co-authored-by: Husam Elbashir <[email protected]>
- Loading branch information
1 parent
3d515a5
commit 69a1b66
Showing
9 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<script setup> | ||
import { GoogleMap, AdvancedMarker } from '@lib' | ||
import { apiPromise } from '@docs/shared' | ||
|
||
const center = { lat: 40.689247, lng: -74.044502 } | ||
</script> | ||
|
||
|
||
# Advanced Marker | ||
|
||
Use the `AdvancedMarker` component to draw markers, drop pins or any custom icons on a map. `AdvancedMarker` is the new version offered by google when deprecated the `Marker` component ([read more here](https://developers.google.com/maps/deprecations#googlemapsmarker_in_the_deprecated_as_of_february_2024)). | ||
|
||
In order to use the `AdvancedMarker` component is necessary to specify a MapId on declaring the `GoogleMap` component ([see more here](https://developers.google.com/maps/documentation/javascript/advanced-markers/start#create_a_map_id)). | ||
|
||
## Options | ||
|
||
You can pass a [AdvancedMarkerElementOptions](https://developers.google.com/maps/documentation/javascript/reference/advanced-markers#AdvancedMarkerElementOptions) object to the `options` prop to configure your marker. | ||
|
||
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. | ||
|
||
```vue | ||
<script setup> | ||
import { GoogleMap, AdvancedMarker } from 'vue3-google-map' | ||
const center = { lat: 40.689247, lng: -74.044502 } | ||
const markerOptions = { position: center, label: 'L', title: 'LADY LIBERTY' } | ||
const pinOptions = { background: '#FBBC04' } | ||
</script> | ||
<template> | ||
<GoogleMap | ||
api-key="YOUR_GOOGLE_MAPS_API_KEY" | ||
mapId="DEMO_MAP_ID" | ||
style="width: 100%; height: 500px" | ||
:center="center" | ||
:zoom="15" | ||
> | ||
<AdvancedMarker :options="markerOptions" :pin-options="pinOptions"/> | ||
</GoogleMap> | ||
</template> | ||
``` | ||
|
||
<ClientOnly> | ||
<GoogleMap | ||
:api-promise="apiPromise" | ||
mapId="DEMO_MAP_ID" | ||
style="width: 100%; height: 500px" | ||
:center="center" | ||
:zoom="15" | ||
> | ||
<AdvancedMarker :options="{ position: { lat: 40.689247, lng: -74.044502 } }" :pin-options="{ background: '#FBBC04' }" /> | ||
</GoogleMap> | ||
</ClientOnly> | ||
## Events | ||
|
||
You can listen for [the following events](https://developers.google.com/maps/documentation/javascript/reference/advanced-markers#AdvancedMarkerElement-Events) on the `AdvancedMarker` component. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { defineComponent, PropType, toRef, provide, computed, inject, markRaw, onBeforeUnmount, ref, watch } from "vue"; | ||
import { markerSymbol, apiSymbol, mapSymbol, markerClusterSymbol } from "../shared/index"; | ||
import equal from "fast-deep-equal"; | ||
|
||
const markerEvents = ["click", "drag", "dragend", "dragstart", "gmp-click"]; | ||
|
||
export default defineComponent({ | ||
name: "AdvancedMarker", | ||
props: { | ||
options: { | ||
type: Object as PropType<google.maps.marker.AdvancedMarkerElementOptions>, | ||
required: true, | ||
}, | ||
pinOptions: { | ||
type: Object as PropType<google.maps.marker.PinElementOptions>, | ||
required: false, | ||
}, | ||
}, | ||
emits: markerEvents, | ||
setup(props, { emit, expose, slots }) { | ||
const options = toRef(props, "options"); | ||
const pinOptions = toRef(props, "pinOptions"); | ||
|
||
const marker = ref<google.maps.marker.AdvancedMarkerElement>(); | ||
|
||
const map = inject(mapSymbol, ref()); | ||
const api = inject(apiSymbol, ref()); | ||
const markerCluster = inject(markerClusterSymbol, ref()); | ||
|
||
const isMarkerInCluster = computed( | ||
() => !!(markerCluster.value && api.value && marker.value instanceof google.maps.marker.AdvancedMarkerElement) | ||
); | ||
|
||
watch( | ||
[map, options, pinOptions], | ||
async (_, [oldMap, oldOptions, oldPinOptions]) => { | ||
const hasOptionChange = !equal(options.value, oldOptions) || !equal(pinOptions.value, oldPinOptions); | ||
const hasChanged = hasOptionChange || map.value !== oldMap; | ||
|
||
if (!map.value || !api.value || !hasChanged) return; | ||
|
||
const { AdvancedMarkerElement, PinElement } = api.value.marker; | ||
|
||
if (marker.value) { | ||
const { map: _, content, ...otherOptions } = options.value; | ||
|
||
Object.assign(marker.value, { | ||
content: pinOptions.value ? new PinElement(pinOptions.value).element : content, | ||
...otherOptions, | ||
}); | ||
|
||
if (isMarkerInCluster.value) { | ||
markerCluster.value?.removeMarker(marker.value); | ||
markerCluster.value?.addMarker(marker.value); | ||
} | ||
} else { | ||
if (pinOptions.value) { | ||
options.value.content = new PinElement(pinOptions.value).element; | ||
} | ||
|
||
marker.value = markRaw(new AdvancedMarkerElement(options.value)); | ||
|
||
if (isMarkerInCluster.value) { | ||
markerCluster.value?.addMarker(marker.value); | ||
} else { | ||
marker.value.map = map.value; | ||
} | ||
|
||
markerEvents.forEach((event) => { | ||
marker.value?.addListener(event, (e: unknown) => emit(event, e)); | ||
}); | ||
} | ||
}, | ||
{ | ||
immediate: true, | ||
} | ||
); | ||
|
||
onBeforeUnmount(() => { | ||
if (marker.value) { | ||
api.value?.event.clearInstanceListeners(marker.value); | ||
|
||
if (isMarkerInCluster.value) { | ||
markerCluster.value?.removeMarker(marker.value); | ||
} else { | ||
marker.value.map = null; | ||
} | ||
} | ||
}); | ||
|
||
provide(markerSymbol, marker); | ||
|
||
expose({ marker }); | ||
|
||
return () => slots.default?.(); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters