8

I am using the vue2-google-map library to display a map with a marker.

<gmap-map ref="mymap" :center="mapStartLocation" :zoom="17" style="width: 100%; height: 300px">
    <gmap-marker v-on:change="updateCoordinates()" :position="mapStartLocation" :draggable="true" @closeclick="updateCoordinates()"/>
</gmap-map>

This works and puts the marker on the coordinates of mapStartLocation

How do I get the new coordinates after the user draggs the marker on the map?

I tried with a @closeClick and v-on:change but neither registers. Even if they would, the question of how to get the new coordinate values remains.

Any pointers on how to operate with this library. Nothing in the documentation.

2 Answers 2

29

I found @drag to be the best solution. However, the event is emitted while the user is dragging, not only after, to which @dragend should work according to source-code, but for some reason doesn't in my case.

<template>
    <gmap-map ref="mymap" :center="mapStartLocation" :zoom="17" style="width: 100%; height: 300px">
        <gmap-marker :position="mapStartLocation" :draggable="true" @drag="updateCoordinates" />
    </gmap-map>
</template>

<script>
export default {
    data() {
        return {
            coordinates: null,
        }
    },
    methods: {
        updateCoordinates(location) {
            this.coordinates = {
                lat: location.latLng.lat(),
                lng: location.latLng.lng(),
            };
        },
    },
}
</script>
5
  • 1
    Thanks for the pointer. How do you pass the coordinate values with this method to the updateCoordinates() method?
    – KasparTr
    May 23, 2018 at 5:24
  • Location is passed as parameter - see updated answer
    – nomadoda
    May 23, 2018 at 14:23
  • Thanks for your answer! Just faced same situation.
    – NickitaX
    May 25, 2018 at 6:27
  • Awesome, thanks for pointing that out, it's nowhere documented though, how can I find that event in the future by myself?
    – jlos
    Dec 18, 2019 at 20:01
  • 1
    There is some documentation here, but could use some work: github.com/Jeson-gk/vue2-google-maps/blob/master/API.md
    – nomadoda
    Dec 19, 2019 at 9:44
8

So, You can add Event on stop drag

<GmapMarker
   :position="jdata.geo"
   :clickable="true"
   :draggable="true"
   @dragend="gMapFunc($event.latLng)"
 </GmapMarker>

////////////////////////

 methods: {
      //set after merker end drag
      gMapFunc(evnt) {
        this.jdata = {"geo": {"lat":evnt.lat(), "lng":evnt.lng()}};
      },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.