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

Ensure Angular JS includes a space or special character after applying a currency filter

Is there a way to include a space or special character after the "₹" symbol? Desired output: ₹ 49 Current output: ₹49 HTML - {{cart.getTotalPrice() | currency:"₹"}} ...

I can't seem to get the post method to work properly for some unknown reason

Hello there, I am encountering an issue while trying to submit a form on my website using the post method. For some reason, it keeps returning a null value. However, when I send data not through the form but instead by reading axios, it works perfectly fin ...

Updating form in react requires a double click

I'm facing an issue with the popup form in my note-taking project. The popup form displays when a user wants to edit a note, and it should already contain the contents of the selected note. While the form shows the correct contents, I've noticed ...

Axios - Show the error message returned from validation as [object Object]

Within my API, I include the following validation logic: $request->validate([ 'firstname' => 'required', 'lastname' => 'required', 'username' => 'required|unique:us ...

What is the best way to add all the items from an array to a div element?

I am currently facing an issue where only the last object in my array is being added to my div using the code below. How can I modify it to add all objects from the array to my div? ajaxHelper.processRequest((response: Array<Vehicle.Vehicle>) ...

Combining Data Structures in Javascript for Visualization in VueJS

Welcome to my first time asking a question. I am currently working on integrating data from two different API endpoints served by a Django Rest Framework backend and displaying it using VueJS on the frontend. The main issue I'm encountering is how t ...

Unable to render Javascript model in THREE JS

I am facing an issue with this code below as I can't seem to load the model. loader = new THREE.JSONLoader(true); loader.load("modelo.js" , function ( geometry, materials) { mesh = new THREE.Mesh( geometry, ...

How can I sort by the complete timestamp when using the Antd table for dates?

I have an item in my possession. const data: Item[] = [ { key: 1, name: 'John Brown', date: moment('10-10-2019').format('L'), address: 'New York No. 1 Lake Park', }, { ...

jQuery function causing lag in speed

Is there a reason why this particular function is running so slowly? If no obvious issue is found, I may have to rule out jQuery as the culprit and focus on my CSS. $("#dialog-modal").dialog({ modal: true, autoOpen: false, resizable: false, ...

How to capture a change event in a dynamically loaded element using JQuery

I am facing an issue with dynamically added rows in my table. Each row contains a select option, and I need to trigger an action when the select is changed. However, since the rows are loaded after the page load, my function does not work as expected. < ...

Utilizing Sequelize validation through condition objects

const db = require("../models"); const Meet = db.meet; checkDuplicateTime = (req, res, next) => { Meet.findAll({ where: { tanggal: req.body.date, waktu: req.body.time } }).then(time => { ...

What is the process for creating a login page that redirects automatically?

For instance: Whenever I enter gmail.com, it automatically takes me to this link: https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmp ...

Changing the bootstrap popover location

I'm looking to customize the position of a Bootstrap popover that appears outside of a panel. Here's my setup: HTML: <div class="panel"> <div class="panel-body"> <input type="text" id="text_input" data-toggle="popover ...

Having trouble declaring a module in an npm package with Typescript?

I'm currently working on a project using Vue.js and TypeScript. Within project "A," I am utilizing a private npm package called "B," which serves as a component library. This package "B" also incorporates another library, 'tiptap,' which unf ...

Unlocking the WiFi Security Key and Accessing Connected Devices with Javascript

When utilizing the command line netsh wlan show interfaces, it displays various information. However, I am specifically interested in extracting the data Profile : 3MobileWiFi-3D71. My goal is to retrieve only the content after the : so that ...

Is my React component being rendered twice?

I have created an app component and a test component. Within both components, I have included a console.log statement in the render method. Upon observation, I noticed that the app component is only rendered once, while the test component renders twice. Ad ...

Modify the name of the document

I have a piece of code that retrieves a file from the clipboard and I need to change its name if it is named image.png. Below is the code snippet where I attempt to achieve this: @HostListener('paste', ['$event']) onPaste(e: ClipboardE ...

Creating TypeScript domain objects from JSON data received from a server within an Angular app

I am facing a common challenge in Angular / Typescript / JavaScript. I have created a simple class with fields and methods: class Rectangle { width: number; height: number; area(): number { return this.width * this.height; } } Next, I have a ...

Is there a way to stop a specific route in Express from continuing further execution without completing its function?

In my post route, I need to ensure that the user has provided all the necessary data in the body. To achieve this, I have added an if block to check for errors. router.post("/", (req, res) => { if(req.body.age < 24) { res.send("You are too you ...

Tips for optimizing the processing speed of large XML files using jQuery, Javascript, and PHP

Currently, I am developing a store overview page that displays about 20 products per page. The data for this page is sourced from a zipped (gzip) XML file (*.xml.gz). You can find the feed here: . Every day, I download this file to my server using PHP and ...