我正在使用vue2谷歌 map 库来显示带有标记的 map .

<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>

这样做可以将标记放在 map 的坐标上

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

我try 了@closeClick和v-on:change,但都没有注册.即使他们愿意,如何获得新坐标值的问题仍然存在.

任何关于如何使用这个库的提示.

推荐答案

我发现@drag是最好的解决方案.然而,事件是在用户拖动时发出的,不仅仅是在之后,@dragend应该是according to source-code,但由于某种原因,在我的情况下不会.

<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>

Vue.js相关问答推荐

在VITE-VUE APP-DEV模式上重定向HTTP请求

Pinia+Vue 3状态react 性-StoreToRef的替代方案

Vuejs 通过数据键迭代复杂对象

如何在Vue3中使用vuechartjs 5.2从父组件更新图表

如何在vue js模板中添加foreach和for循环

如何在 Nuxt3 中使用 nuxtjs/auth-next 模块?

有什么理由不在 vue3 中使用