Vue-谷歌地图自动完成两种方式绑定不起作用

时间:2018-10-04 05:42:21

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

我正在使用vue-google-maps,并且已经实现了该插件,并且可以完美运行,但是存在一个小问题(或者我可能无法正确执行)。当我实现自动完成功能时,它会以一种方式起作用。我输入了地址,地图将我指向了所选位置,但是当我拖动指针时,它不会更新搜索字段中的地址。 我正在使用Vuejs 2 vue-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>

2 个答案:

答案 0 :(得分:0)

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

在您的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)
       }
   })
}

答案 1 :(得分:0)

将“ ref”添加到您的自动完成组件中,例如:

def select_input_file():
    #...
    app = TestApp(root, input_file_path)
    app.place(bordermode = INSIDE,height = 500, width = 2000, x =0, y=50)

    df = app.table     # contains the updated table which can be passed to other functions
    newFunc( df )      # sample call

将updateMarker函数更改为此:

<GmapAutocomplete ref="gmapAutocomplete" :position.sync="markers[0].position" @keyup.enter="usePlace" @place_changed="setPlace">
        </GmapAutocomplete>