【发布时间】:2018-12-02 13:24:09
【问题描述】:
在我的 Vue 应用程序中,我展示了一个带有标记的 Google 地图,这些标记是使用 Axios 从 API 检索的。当我单击一个标记时,它会显示一个带有按钮的信息窗口,其中有一个 @click='show_details = !show_details' 来切换带有详细信息的附加 div。但是,这不起作用,我不知道为什么。这是代码:
<template>
<div class="map">
<div class="google-map" id="map"></div>
<div class="row justify-content-end">
<div class="col-2">
<div class="visit-details" v-show="show_details">details</div>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'Map',
data(){
return {
lat: 54.6,
lng: -2,
show_details: false
}
},
methods: {
renderMap(){
const map = new google.maps.Map(document.getElementById('map'), {
center: { lat: this.lat, lng: this.lng },
zoom: 6,
maxZoom: 20,
minZoom: 3
})
axios.get('https://bla.json')
.then(res => {
res.data.forEach(doc => {
let contentString = "<div @click='show_details = !show_details'>details</div>";
let visitLatLng = {lat: doc.latitude, lng: doc.longitude};
let marker = new google.maps.Marker({
position: visitLatLng,
title: doc.ground,
map: map
})
let infowindow = new google.maps.InfoWindow({
content: contentString
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
})
})
.catch(error => console.log(error))
}
},
mounted(){
this.renderMap()
}
}
</script>
<style>
</style>
【问题讨论】:
-
我不知道 vue.js,但看起来与 here 解释的问题相同。
-
不确定@click="" 是否正在编译,如果正在编译,请尝试@click.native=""。
-
不幸的是,@click.native 并没有什么不同。
-
它似乎没有渲染,所以也许使用github.com/xkjyeah/vue-google-maps 是一个更好的选择。
标签: google-maps vue.js