Switching Visibility of Map Layers through an External Component

Let me start by mentioning that I am a design student utilizing Vue.js for prototyping my senior project. This is merely a prototype of a diary app and not an actual working project.

The issue at hand involves a map component created with Vue2Leaflet, which includes a Tile Layer. On top of the Tile Layer, I have rendered a GeoJSON file containing two sets of coordinates.

In addition, there is a datepicker component that emits values through an EventBus.

The goal is to toggle the visibility of different <l-geo-json> components based on the emitted value from the datepicker. The visibility parameter of each <l-geo-json> is controlled by changing it according to the boolean value from the datepicker. While this parameter does change dynamically, it does not reflect in the map rendering.

I suspect that this might be due to the map component not re-rendering properly.

This is how my map component looks:

<template>
  <div v-if="refresh" id="MapView2">
    <i class="material-icons geoLocate" v-on:click="geoLoc">location_searching</i>
    <l-map :zoom="zoom" :options="{ zoomControl: false }" class="map" :center="center">
      <l-tile-layer :url="url" :attribution="attribution"></l-tile-layer>
      <l-geo-json :visible="yesterday.day" :geojson="bus.geojson" :options="bus.options"></l-geo-json>
      <l-geo-json :visible="today.day" :geojson="today.geojson" :options="today.options"></l-geo-json>
    </l-map>
  </div>
</template>

... more code ...

Furthermore, my geojson.js file includes:

    import Vue from 'vue';
    export const EventBus2 = new Vue();
    export const data = {
      today: {
        "type": "FeatureCollection",
        "id": "0520",
        "visible": false,
        "features": [ // geojson features and coordinates ]
      },

      yesterday: {
        "type": "FeatureCollection",
        "id": "0520",
        "visible": false,
        "features": [ // geojson features and coordinates ]
      }
  }

The datepicker emits values in MMDD format and the switch statement checks these values against the id property in the GeoJSON data. If a match is found, the corresponding visible property is changed. Although this property is updated upon emission from the datepicker, the changes are not reflected in the map display.

How can this issue be resolved? Is using watch or another method advisable, and if so, how should it be implemented?

Answer №1

To effectively update your instance, you should directly modify the data.

To avoid confusion, consider importing from a different file as data2, instead of using the same name which might be causing issues.

import { data as data2, EventBus2 } from '../assets/geojson/sample-geojson.js';

If your GeoJSON layers do not dynamically change visibility, it's because you initialize your data and day properties using information from data2, but they are not bound to future changes in data2.

A simple solution would be to toggle the data and day properties in your event listener for "mapDay", instead of referring to data2.

EventBus2.$on('mapDay', pickerValue => {
  switch (pickerValue) {
    case data2.today.id:
      this.yesterday.day = true;
      this.today.day = false;
    break;
    case data2.dun.id:
      this.yesterday.day = false;
      this.today.day = true;
    break;
  }
});

In cases where you want both data and data2 to be synced, you can toggle them together. However, keep in mind that changing data2 in another component will not reflect here.

Alternatively, simplify your logic by storing pickerValue in the data instance and comparing it to geojson.id within the binding expression:

:visible="pickerValue === yesterday.geojson.id"

For a more modular approach, expose the pickerValue as a prop so you can modify it at the parent level without relying on events like "mapDay".

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

Navigate through the Jquery slider by continuously scrolling to the right or simply clicking

Is there a way to prevent my slider from continuously scrolling? I think it has something to do with the offset parameter but I'm having trouble figuring it out. Any assistance would be greatly appreciated. var $container = $(container); var resizeF ...

Issue with inconsistent indentations in Pug template

Dealing with the Pug template engine has been a frustrating experience. Despite spending an entire day trying to figure it out, all I got was errors about inconsistent indentations. It's disheartening when my text ends up in the "our sponsor section" ...

Utilizing consistent CSS styles across VueJS components

Currently tackling a VueJS 2 project and facing issues with scoped styling when cleaning up the code. My setup involves 3 components that share similarities, prompting me to utilize mixins for consolidating the code into one file. Each component makes use ...

Does jQuery UI Layout have compatibility issues with jQuery version 3.3.1?

jQuery UI Layout is compatible with older versions of jQuery, but not with newer versions... Here is a working example: <html> <head> <script src="http://layout.jquery-dev.net/lib/js/jquery-1.4.2.js"></script> <script ...

Navigating a secure Koa authentication flow using compose mechanism

I have this isAuthenticated function in expressjs that composes middleware into one. Now, I need to achieve the same functionality in Koa as I am migrating from Express. How can I replicate this in Koa? import compose from 'composable-middleware&apos ...

What is the best method for incorporating a JavaScript redirect into an Android WebView?

My issue involves loading a page into a webview. Typically, when I use the code webview.loadUrl(url); it functions as expected. The url contains a javascript code that redirects the page. Here is the javascript code: <script type="text/javascript"> ...

Utilizing a Custom Validator to Compare Two Values in a Dynamic FormArray in Angular 7

Within the "additionalForm" group, there is a formArray named "validations" that dynamically binds values to the validtionsField array. The validtionsField array contains three objects with two values that need to be compared: Min-length and Max-Length. F ...

Show the current server time on the client side using Meteor

Is there a more efficient way to display the server's time as a running clock (h:m:s) on the client using Meteor? Traditional JavaScript/PHP methods involve fetching the server time periodically and calculating the time difference with the client. Bu ...

Minimum number of coins required for a specific amount

I need assistance creating a JavaScript/jQuery function to determine the minimum number of coins required to reach a specified total amount. Here is an array object containing different coin values: var coins = [ { pennies: 200, prin ...

Ways to Verify the Existence of a Value in an Array Using JavaScript

In my Laravel blade file, I have an array that I am accessing in JavaScript like this: var data = {!! json_encode($data) !!}; When I check the console, the variable is displayed as seen here: variable data console print Additionally, I'm retrieving ...

What is the best way to store React form data in a queue for future processing?

I am currently developing a ticketing system in React, utilizing Node.js for the backend and Python for the email server. My goal is to process form submissions by extracting all the form data, creating an instance of a plain JavaScript class with only a ...

Step-by-step guide for inputting information into a designated user profile

Having recently started working with Firebase and Vue.js, I encountered an issue on how to include symptoms in the user collection based on their ID. Check out how it looks in the Firebase User collection here. This is the user interface where users can s ...

Vue zooming element

Struggling to figure out how to implement this plugin for zooming and panning multiple components. I am interested in using the https://github.com/timmywil/panzoom library as it seems like a good option. Just started playing around with it and my initial ...

I am looking to initiate the animation by triggering the keyframe upon clicking a button

Is there a way to create an animation triggered by a button click instead of loading the page? Each table data cell has its own class with a unique keyframe defining the style changes over time. @-webkit-keyframes fadeIn { from { opacity:0; ...

Using JavaScript, you can add an article and section to your webpage

Currently, I am utilizing AJAX to showcase posts. My objective is to extract each property from a JSON object that is returned and generate an <article> for the title followed by a subsequent <section> for the content. While I can successfully ...

jQuery Triggered Download Resulting in 'Error: Connection Issue'

I need to trigger a download using a JQuery AJAX request once it's finished. EXAMPLE CODE: $('.getPDF').click(function(){ var filepath = 'localhost:3000/pdf/formula-' + this.id + '.pdf'; $.ajax({ url: '/formu ...

Experience the enhanced features and optimized performance of Onsen 2.0 compared to the earlier version

Apologies if this question is too simplistic, but I am finding some conflicting information in the documentation about using Onsen with Monaca. I am currently utilizing Monaca cloud and I prefer to work solely with pure JS, without incorporating Angular ...

How to toggle a class using ng-click in AngularJS

Currently, I am working on a project using AngularJS for my website. One specific feature involves having a form that is initially hidden with "display:none" set deliberately. There's also a button labeled create. My goal is to have the form toggle b ...

What is the process for obtaining a compilation of JavaScript files that are run when an event is triggered?

How can I generate a list of all the JavaScript files that are triggered when a button is clicked or an input field is selected in a complex system? Is it possible to achieve this with Chrome DevTools, or are there alternative solutions available? If Chro ...

Include two additional elements for action in a list

In my React project, I've created a list with the following elements: Avatar Text Edit icon Delete icon I managed to set up the structure successfully up until the delete icon. Now, how can I properly add it without overlapping the edit icon? Both ...