javascript - Vue-google-map 自动完成两种方式绑定(bind)不起作用

标签 javascript google-maps vue.js vuejs2 google-maps-markers

我正在使用 vue-google-maps ,并且我已经实现了该插件并且它可以完美地工作,但是有一个小问题(或者我无法正确完成)。当我实现自动完成功能时,它以一种方式工作。我输入地址, map 将我指向所选地点,但当我拖动指针时,它不会更新搜索字段中的地址。 我正在使用 Vuejs 2vue-google-maps

这就是我正在做的:

<template>
    <div>
        <label>
            AutoComplete
            <GmapAutocomplete :position.sync="markers[0].position" @keyup.enter="usePlace" @place_changed="setPlace">
            </GmapAutocomplete>
            <button @click="usePlace">Add</button>
        </label>
        <br/>

        <gmap-map
            :center="center"
            :zoom="7"
            map-type-id="terrain"
            style="width: 100%; height: 500px"
        >
            <gmap-marker
                @dragend="updateMaker" 
                :key="index"
                v-for="(m, index) in markers"
                :position="m.position"
                :clickable="true"
                :draggable="true"
                @click="center=m.position"
            ></gmap-marker>

        </gmap-map>

    </div>
</template>

<script>

    export default {
        data() {
            return {
                center: {lat: 10.0, lng: 10.0},
                markers: [{
                    position: {lat: 11.0, lng: 11.0}
                }],
                place: null,
            }
        },
        description: 'Autocomplete Example (#164)',
        methods: {
            setPlace(place) {
                this.place = place
            },
            setDescription(description) {
                this.description = description;
            },
            usePlace(place) {
                if (this.place) {
                    var newPostion = {
                        lat: this.place.geometry.location.lat(),
                        lng: this.place.geometry.location.lng(),
                    };
                    this.center = newPostion;
                    this.markers[0].position =  newPostion;
                    this.place = null;
                }
            },
            updateMaker: function(event) {
                console.log('updateMaker, ', event.latLng.lat());
                this.markers[0].position = {
                    lat: event.latLng.lat(),
                    lng: event.latLng.lng(),
                }
            },
        }
    }
</script>

最佳答案

我不确定这是否受支持。您可能必须自己实现。

在您的 updateMaker() 方法中,您可以使用 google.maps.Geocoder() 自己查找地址名称:

updateMaker: function(event) {
   const geocoder = new google.maps.Geocoder()
   geocoder.geocode({ 'latLng': event.latLng }, (result, status) => {
       if (status === google.maps.GeocoderStatus.OK) {
         // set the input field value with address:
         console.log(result[0].formatted_address)
       }
   })
}

关于javascript - Vue-google-map 自动完成两种方式绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52639743/

相关文章:

javascript - 在Vue.js中,如何设置一段数据始终等于包含元素的高度?

JavaScript:从 JSON 数组构建自定义数据树

javascript - 使用indexOf()检查JS数组中是否存在特定对象

javascript - 我可以实时检查 NodeJS 中的标准输出吗?

r - 根据纬度和经度设置ggmap边界

android - 无法解析 fragment 内的方法 'getSupportFragmentManager ( )'

javascript - 将多个 HTML 输入更改为不同的值

javascript - 如何在谷歌地图上找到经纬度的最小值和最大值

javascript - setTimeout方法,(交换组件)

javascript - Vue 将类添加到目标但从其他实例中删除