|
| 1 | +--- |
| 2 | +title: Programmatic API |
| 3 | +--- |
| 4 | + |
| 5 | +The `ScriptGoogleMaps` component exposes its internal APIs via template ref, giving you full control beyond what the declarative SFC components provide. |
| 6 | + |
| 7 | +## Accessing the API |
| 8 | + |
| 9 | +```vue |
| 10 | +<script setup lang="ts"> |
| 11 | +const mapRef = ref() |
| 12 | +
|
| 13 | +function onReady({ googleMaps, map, createAdvancedMapMarker, resolveQueryToLatLng, importLibrary }) { |
| 14 | + // All APIs available here |
| 15 | +} |
| 16 | +</script> |
| 17 | +
|
| 18 | +<template> |
| 19 | + <ScriptGoogleMaps ref="mapRef" @ready="onReady" /> |
| 20 | +</template> |
| 21 | +``` |
| 22 | + |
| 23 | +### Exposed Properties |
| 24 | + |
| 25 | +| Property | Type | Description | |
| 26 | +|---|---|---| |
| 27 | +| `googleMaps` | `Ref<typeof google.maps>`{lang="html"} | The Google Maps API namespace | |
| 28 | +| `map` | `ShallowRef<google.maps.Map>`{lang="html"} | The map instance | |
| 29 | +| `createAdvancedMapMarker` | `(options?) => Promise<AdvancedMarkerElement>`{lang="html"} | Create a marker programmatically | |
| 30 | +| `resolveQueryToLatLng` | `(query: string) => Promise<LatLng>`{lang="html"} | Geocode a location string | |
| 31 | +| `importLibrary` | `(key: string) => Promise<any>`{lang="html"} | Load an additional Google Maps library | |
| 32 | + |
| 33 | +## Creating Markers Programmatically |
| 34 | + |
| 35 | +For cases where declarative `v-for` markers aren't flexible enough (dynamic data, imperative creation logic), use `createAdvancedMapMarker`: |
| 36 | + |
| 37 | +```vue |
| 38 | +<script setup lang="ts"> |
| 39 | +const mapRef = ref() |
| 40 | +
|
| 41 | +async function addMarkerAtCenter() { |
| 42 | + const map = mapRef.value.map.value |
| 43 | + const center = map.getCenter() |
| 44 | + const marker = await mapRef.value.createAdvancedMapMarker({ |
| 45 | + position: { lat: center.lat(), lng: center.lng() }, |
| 46 | + title: 'New Marker', |
| 47 | + }) |
| 48 | +} |
| 49 | +</script> |
| 50 | +
|
| 51 | +<template> |
| 52 | + <ScriptGoogleMaps ref="mapRef" :center="{ lat: -33.8688, lng: 151.2093 }" :zoom="12" /> |
| 53 | + <button @click="addMarkerAtCenter"> |
| 54 | + Add Marker at Center |
| 55 | + </button> |
| 56 | +</template> |
| 57 | +``` |
| 58 | + |
| 59 | +::callout |
| 60 | +For most use cases, prefer the declarative `ScriptGoogleMapsAdvancedMarkerElement` component with `v-for`. Use the programmatic API when you need fine-grained control over marker lifecycle or are integrating with external data sources. |
| 61 | +:: |
| 62 | + |
| 63 | +## Geocoding Queries |
| 64 | + |
| 65 | +Convert location strings to coordinates using `resolveQueryToLatLng`. When you enable the registry proxy, this resolves server-side (cheaper, API key hidden). Otherwise it falls back to the client-side Places API. |
| 66 | + |
| 67 | +```vue |
| 68 | +<script setup lang="ts"> |
| 69 | +const mapRef = ref() |
| 70 | +
|
| 71 | +async function goToLocation(query: string) { |
| 72 | + const latLng = await mapRef.value.resolveQueryToLatLng(query) |
| 73 | + mapRef.value.map.value.setCenter(latLng) |
| 74 | + mapRef.value.map.value.setZoom(15) |
| 75 | +} |
| 76 | +</script> |
| 77 | +
|
| 78 | +<template> |
| 79 | + <ScriptGoogleMaps ref="mapRef" :center="{ lat: 0, lng: 0 }" :zoom="2" /> |
| 80 | + <button @click="goToLocation('Eiffel Tower, Paris')"> |
| 81 | + Go to Paris |
| 82 | + </button> |
| 83 | +</template> |
| 84 | +``` |
| 85 | + |
| 86 | +## Importing Libraries |
| 87 | + |
| 88 | +Google Maps splits functionality into libraries that load on demand. Use `importLibrary` to access geometry, drawing, places, and visualization APIs: |
| 89 | + |
| 90 | +```vue |
| 91 | +<script setup lang="ts"> |
| 92 | +const mapRef = ref() |
| 93 | +
|
| 94 | +async function measureDistance(a: google.maps.LatLng, b: google.maps.LatLng) { |
| 95 | + const geometry = await mapRef.value.importLibrary('geometry') |
| 96 | + return geometry.spherical.computeDistanceBetween(a, b) |
| 97 | +} |
| 98 | +</script> |
| 99 | +``` |
| 100 | + |
| 101 | +Available libraries: `marker`, `places`, `geometry`, `drawing`, `visualization` |
| 102 | + |
| 103 | +## Subscribing to Map Events |
| 104 | + |
| 105 | +Use the `@ready` event to access the map instance and subscribe to native Google Maps events: |
| 106 | + |
| 107 | +```vue |
| 108 | +<script setup lang="ts"> |
| 109 | +function onReady({ map }) { |
| 110 | + watch(map, (m) => { |
| 111 | + if (!m) |
| 112 | + return |
| 113 | + m.addListener('zoom_changed', () => { |
| 114 | + console.log('Zoom:', m.getZoom()) |
| 115 | + }) |
| 116 | + m.addListener('center_changed', () => { |
| 117 | + console.log('Center:', m.getCenter()?.toJSON()) |
| 118 | + }) |
| 119 | + m.addListener('idle', () => { |
| 120 | + // Map finished moving, good time to fetch data for visible bounds |
| 121 | + const bounds = m.getBounds() |
| 122 | + }) |
| 123 | + }, { immediate: true }) |
| 124 | +} |
| 125 | +</script> |
| 126 | +
|
| 127 | +<template> |
| 128 | + <ScriptGoogleMaps @ready="onReady" /> |
| 129 | +</template> |
| 130 | +``` |
0 commit comments