简体   繁体   English

Vue:单击一个时打开所有GmapInfoWindows

[英]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:解决此问题的一种方法是:

  1. Modify openWindow method to:修改openWindow方法为:
openWindow(index) {
  this.window_open = this.window_open === index ? null : index;
},
  1. Change you opened prop from :opened="window_open" to :opened="window_open === index"将您opened的道具从:opened="window_open"更改为:opened="window_open === index"
  2. Call your openWindow method with openWindow(index) .使用openWindow(index)调用您的openWindow方法。
  3. I am suggesting changing the name openWindow to toggleWindow since you are not only showing it but hiding too.我建议将名称openWindow更改为toggleWindow因为您不仅要显示它,还要隐藏它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM