[英]Vue: All GmapInfoWindows open when one is clicked
I am making a website where you can report crimes/incidents by placing a marker on a map and then the markers with all the incidents show on a different map.我正在创建一个网站,您可以通过在 map 上放置一个标记来报告犯罪/事件,然后将所有事件的标记显示在不同的 map 上。
Every marker has an info window that shows the incident and the time it was reported.每个标记都有一个信息 window 显示事件和报告时间。
The problem I am having is that when I click one of the markers to open its info window, all of the info windows open.我遇到的问题是,当我单击其中一个标记以打开其信息 window 时,所有信息 windows 都会打开。
Here is my code:这是我的代码:
<template>
<div>
<GmapMap
ref="mapRef"
:center="{ lat: 10, lng: 10 }"
:zoom="11"
style="width: 500px; height: 300px"
>
<GmapMarker
:key="index"
v-for="(m, index) in this.markers"
:position="m.position"
:clickable="true"
@click="openWindow"
>
<GmapInfoWindow
:opened="window_open"
:position="m.position"
>
Incident: {{ m.incident }}<br />
Time: {{ m.time }}
</GmapInfoWindow>
</GmapMarker>
</GmapMap>
</div>
</template>
<script>
import { gmapApi } from "vue2-google-maps";
import { db } from "../main";
export default {
name: "MapComponent",
data() {
return {
markerCollection: [],
markers: [],
info_marker: null,
window_open: false,
};
},
firestore() {
return { markers: db.collection("Reports") };
},
methods: {
openWindow() {
if (this.window_open) {
this.window_open = false;
} else {
this.window_open = true;
}
},
},
mounted() {
// At this point, the child GmapMap has been mounted, but
// its map has not been initialized.
// Therefore we need to write mapRef.$mapPromise.then(() => ...)
this.$refs.mapRef.$mapPromise.then((map) => {
map.panTo({ lat: 10, lng: 10 });
});
},
computed: {
google: gmapApi,
},
};
</script>
I've seen some people's code that use GmapInfoWindows but they all just have one each, and didn't express any problems with it.我见过一些人的代码使用 GmapInfoWindows,但他们每个人都只有一个,并且没有表达任何问题。 And I have tried their code and I get this issue.
我已经尝试了他们的代码,我得到了这个问题。 I have also tried a lot of other things with the bools.
我还用布尔值尝试了很多其他的东西。
Any solution for this?有什么解决办法吗?
The problem is that all of your GmapInfoWindow
are attached to one flag condition: window_open
.问题是您的所有
GmapInfoWindow
都附加到一个标志条件: window_open
。
So if you change window_open
to true
, all of them gets to show.因此,如果您将
window_open
更改为true
,它们都会显示出来。
One way to fix that would be:解决此问题的一种方法是:
openWindow
method to:openWindow
方法为:openWindow(index) {
this.window_open = this.window_open === index ? null : index;
},
opened
prop from :opened="window_open"
to :opened="window_open === index"
opened
的道具从:opened="window_open"
更改为:opened="window_open === index"
openWindow
method with openWindow(index)
.openWindow(index)
调用您的openWindow
方法。openWindow
to toggleWindow
since you are not only showing it but hiding too.openWindow
更改为toggleWindow
因为您不仅要显示它,还要隐藏它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.