Skip to content

Need help on: Change map center or marker position reactivity #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 3 tasks
darkons opened this issue Mar 26, 2025 · 2 comments
Open
1 of 3 tasks

Need help on: Change map center or marker position reactivity #350

darkons opened this issue Mar 26, 2025 · 2 comments

Comments

@darkons
Copy link

darkons commented Mar 26, 2025

What you try to do

Want to change the map center and marker position after geocode an address.

What is the problem

The only way I was found to do that looks like very tricky for me. I think there is a better way.

Steps to reproduce

This is my actual code. Is using panTo and markerPromise the only way?

<script setup>
import { useForm } from '@inertiajs/vue3'
import { onMounted, ref } from 'vue'
import useGoogleMapsUtils from '@/Composables/useGoogleMapsUtils'
import { useMapPromise } from '@gmap-vue/v3/composables'

const props = defineProps({
  address: Object,
})

const { geocodeByLatLng } = useGoogleMapsUtils()

const mapRef = ref(null)
const markerRef = ref(null)
const mapReady = ref(false)

const form = useForm({
  location: props.address.location,
})

onMounted(async () => {
  await useMapPromise('map')
  mapReady.value = true
})

const setAddress = async (event) => {
  const address = await geocodeByLatLng(event)

  form.location = [address.location.lng, address.location.lat]

  mapRef.value.panTo({
    lat: address.location.lat,
    lng: address.location.lng,
  })

  markerRef.value.markerPromise.then((marker) => {
    marker.position = {
      lat: address.location.lat,
      lng: address.location.lng,
    }
  })
}

const submit = () => {
  form.patch(route('address.update'))
}
</script>

<template>
    <div
        <GmvAutocomplete @place_changed="setAddress" />
    </div>

    <div>
        <div v-if="mapReady === false">
            Loading map...
        </div>

        <GmvMap
            ref="mapRef"
            map-key="map"
            :class="'h-96"
            :center="{ lat: form.location[1], lng: form.location[0] }"
            :zoom="14"
        >
            <GmvMarker
                ref="markerRef"
                map-key="map"
                :position="{ lat: form.location[1], lng: form.location[0] }"
                :gmp-draggable="true"
                @update:position="setAddress"
            />
        </GmvMap>
    </div>
</template>

Os

  • Mac Os X
  • Linux (write here the name and version of your distribution)
  • Windows

Plugin version

  • version: 2.1.5
@diegoazh
Copy link
Owner

diegoazh commented Apr 7, 2025

Hi @darkons, your approach looks good. How do you think/want this should be improved?
Or what is the issue that you see on the above implementation?

@darkons
Copy link
Author

darkons commented Apr 7, 2025

Thanks for replying @diegoazh

Apparently the way I did it is the only possible way.
I just thought that the center of GmvMap and the position property of GmvMarker would be updated automatically when changing the value of form.location.

Correct me if I am wrong.

Kind regards!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants