App.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<script setup> import { ref } from 'vue' import { GoogleMap, Marker } from 'vue3-google-map' const center = { lat: 40.689247, lng: -74.044502 } const zoom = 15 // Store markers in a ref to dynamically update them const markers = ref([]) // Add a new marker at the clicked position const addMarker = (event) => { const latLng = event.latLng markers.value.push({ position: { lat: latLng.lat(), lng: latLng.lng() }, label: 'New Marker', title: `Marker at ${latLng.lat()}, ${latLng.lng()}`, }) } </script> <template> <GoogleMap api-key="##yourownapikey##" style="width: 100%; height: 100vh" :center="center" :zoom="zoom" @click="addMarker" > <!-- Render all markers dynamically --> <Marker v-for="(marker, index) in markers" :key="index" :options="marker" /> </GoogleMap> </template> |