Vue: Opening all GmapInfoWindows simultaneously upon clicking one

I am working on a platform where users can report crimes or incidents by placing markers on a map. These markers with all the reported incidents are then displayed on another map.

Each marker has an info window that provides details about the incident and the time it was reported.

The issue I am encountering is that when I click on one marker to view its info window, all of the info windows open simultaneously.

Take a look at my code snippet:

<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() {
    // After mounting the child GmapMap, initialize the map.
    // Use mapRef.$mapPromise.then(() => ...) for this.

    this.$refs.mapRef.$mapPromise.then((map) => {
      map.panTo({ lat: 10, lng: 10 });
    });
  },
  computed: {
    google: gmapApi,
  },
};
</script>

I have reviewed other implementations using GmapInfoWindows, but they only had one each and did not face similar issues. Even after testing their codes, the problem persists. I have experimented with various approaches involving boolean values as well.

Any suggestions to solve this issue?

Answer №1

The issue lies in the fact that all instances of your GmapInfoWindow are tied to a single flag condition: window_open.

As a result, changing the value of window_open to true will cause all of them to display simultaneously.

To resolve this, consider the following steps:

  1. Revise the openWindow method as follows:
openWindow(index) {
  this.window_open = this.window_open === index ? null : index;
},
  1. Update the opened prop from :opened="window_open" to
    :opened="window_open === index"
  2. Invoke the openWindow method with openWindow(index).
  3. I recommend renaming the openWindow function to toggleWindow, considering it not only shows but also hides the window.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring a JSON file to generate a customized array for implementing autocomplete functionality in jQuery

I'm currently working on developing an autocomplete feature for a search bar that will display names of places in Norway. The data is being fetched from a REST API URL provided by a third party. As an example, when the input is "st" there are two res ...

What is the best way to determine the P/E ratio for a specific stock?

Need help with a formula calculation. I have the value for net worth, but I am having trouble iterating over the EPS values and multiplying them by the shares held. Can anyone suggest a solution? Thank you! You can find the Plunker here <div&g ...

Trouble with Updating Div Content?

Within my HTML code, I have included this JavaScript snippet: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script> <script> var auto_refresh = setInterval( function() { $('#loaddiv').fadeOut ...

Facing issues with ReactCSSTransitionGroup as transitionAppear feature is not functioning

I'm having trouble with the appearance and disappearance of notifications. The transitionAppear property doesn't seem to be working as expected. I'm adding the notification popup item to the onBlur event. The animation for the leave event wo ...

What is the best way to display a <div> depending on the screen size in React JS using a global variable, without utilizing state management?

I am attempting to display a message based on the screen resolution in ReactJS. I am utilizing a function component and trying to accomplish this without using state, by using a global variable. const isDesktop = window.innerWidth; {isDesktop > 768 ? ...

The socket.rooms value is updated before the disconnection listener finishes its process

When dealing with the "disconnect" listener, it's known that access to socket.rooms is restricted. However, I encountered an issue with my "disconnecting" listener. After making some modifications to my database within this callback, I attempted to em ...

Incorporate several websites into a React application without using iframes

After developing a React application with create-react-app, I attempted to embed another website onto the app using an iframe. Everything worked perfectly until I wanted to resize the iframe to fit the page, resulting in a Blocked a frame with origin from ...

Is it possible to determine when a Vue.js component instance has been successfully mounted?

Can we access a boolean in each component instance to determine when a component has been mounted? Something along the lines of: <template> <div> <span v-if="$mounted">I am mounted</span> <span v-if="$created">I am ...

Reusing methods in Javascript to create child instances without causing circular dependencies

abstract class Fruit { private children: Fruit[] = []; addChild(child: Fruit) { this.children.push(child); } } // Separate files for each subclass // apple.ts class Apple extends Fruit { } // banana.ts class Banana extends Fruit { } ...

Instructions on opening a modal and changing the source of the iframe within the modal

I currently have a modal within my application that is triggered by Bootstrap: <div class="modal full-page fade" tabindex="-1" role="dialog" id="fullpageModal"> <div class="full-page-content"> <button type="button" class="close" d ...

Error message received when attempting to remove object in Laravel and Vue framework

My aim is to delete a record in Vue successfully. Although the deletion of records works, I am encountering an error message in the console. 405 (Method Not Allowed) The error appears in the network tab as: "exception": "Symfony\Component\ ...

Updating the button text in Angular 7

Here's a question: <button (click)="activateMotion(1)"> <img class="emotion-icon" id="positive-icon" src="" /> </button> <button (click)="activateMotion(-1)"> <img class="emotion-icon" id="negative-icon" src="" /&g ...

Execute an asynchronous function in Javascript, then output the returned data to the console

Is there a way to effectively handle the data returned from an async function? example: JS FILE: async function getData(){ try { $.getJSON('./data.json', (data) => { return data; }); } catch(error ...

Utilizing fibrous in node.js to efficiently fetch data from the request library

I am struggling to efficiently utilize the fibrous library in conjunction with request for obtaining the body of an HTTP request synchronously. However, I have encountered difficulties in managing the flow. In order to address this issue, I created a simp ...

Incorporate the jquery lazy load plugin alongside ZURB foundation data-interchange for optimal performance

Currently, I am engaged in a project that involves utilizing the ZURB foundation framework alongside its data-interchange feature to display various images based on different screen sizes. To learn more about this method, please visit: You can also explo ...

Strategies for temporarily storing values within md-list-item in AngularJS

I am attempting to populate a list with items using material icons. The issue is that the values are being added permanently when the material icon is clicked, disregarding the save and discard buttons at the bottom of the card. My goal is to add values te ...

Tips for presenting hierarchical information from my database in ejs

I'm currently working on a genealogy application using node js express and ejs but I'm facing an issue with displaying the database in order (starting from parent). This is the code snippet for retrieving my data and what I see when I log the ou ...

What is the best way to showcase data from input fields within a bootstrap modal dialog?

After the user has entered their details and clicks submit, I would like to present the information in a Bootstrap modal with a confirmation button below. This serves as a preview of the data before it is saved to the database. Here's what I have so ...

Why is the state object empty in this React Native function?

One React-Native component I have is responsible for rendering an image gallery in a list format. This component is called once for each item on the parent list. It takes two props: "etiqueta," which contains the title of the item, and "galleries," which i ...

Manipulating prop values through dropdown selection

I'm currently working on implementing filtering based on a prop value that changes according to the dropdown selection. Here's my progress so far: template(v-for="field in tableFields") th(:id="field.name") select(@change="filterScope(sc ...