Once map layers have been loaded initially, they will not render again

I'm currently incorporating Mapbox GL into my Vue project using VueMapbox (0.4.1).

<template>
  <MglMap
    :accessToken="accessToken"
    :mapStyle.sync="mapStyle"
    :repaint="true"
    @load="onMapLoaded">
    <MglMarker
      :coordinates="someCoordinates"
      class="map-marker-wrapper">
      <div
        slot="marker"
        class="map-marker">
      </div>
    </MglMarker>
  </MglMap>
</template>

<script>
import Mapbox from 'mapbox-gl'
import { MglMap, MglMarker } from 'vue-mapbox'
import * as MAP from '@/constants/map'

// Vue-Mapbox documentation: https://soal.github.io/vue-mapbox/guide/basemap.html#adding-map-component

export default {
  name: 'Map',
  components: {
    MglMap,
    MglMarker
  },
  props: {
    someCoordinates: {
      type: Array,
      required: true
    },
    darkMode: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      accessToken: MAP.ACCESS_TOKEN,
      mapbox: null,
      map: null,
      actionsDispatcher: null
    }
  },
  computed: {
    mapStyle () {
      return this.darkMode ? MAP.COLOR_PROFILES.DARK : MAP.COLOR_PROFILES.LIGHT
    }
  },
  created () {
    this.mapbox = Mapbox
  },
  methods: {
    async onMapLoaded (event) {
      this.map = event.map
      this.actionsDispatcher = event.component.actions
    
      await this.actionsDispatcher.flyTo({
        center: this.someCoordinates
      })
    }
  }
}
</script>

Upon initial load, everything functions correctly:

https://i.sstatic.net/x30jF.jpg

However, when navigating to a different pseudo-route (e.g., from /#/Map to /#/Profile and back), some specified map layers such as roads and city names are no longer rendered, showing default layers instead. Additionally, the map fails to update to any changes in the mapStyle URL, despite using mapStyle.sync in the template.

https://i.sstatic.net/kFuVC.jpg

Refreshing the browser reinstates the layers properly since the entire app is loaded from scratch, but this workaround is not an ideal solution.

If you have any suggestions or ideas, they would be greatly appreciated.

Answer №1

I was able to solve the issue by identifying a problem with the vue-mapbox example. The variable map (this.map) was being set by "event.map" which led to an error due to the card not refreshing subsequently.

In my scenario, I simply removed that line of code (this.map = event.map) in my onMapLoaded function and it worked perfectly.

Wishing you a pleasant day ahead.

Answer №2

Although I may not be familiar with Vue.js syntax, the issue you're encountering stems from creating your layers within the map.on('load', ... event, which only occurs once. Consequently, when a style change occurs, all layers in the map style (including those created by custom code) are removed.
To recreate your layers upon a style change, it's essential to utilize the event map.on('style.load', .... However, based on your vue.js code snippet, it doesn't seem like this is being implemented. If you provide the portion of the code where vue.js invokes these methods, it would greatly aid in resolving your issue.

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

Should we pass <script> tags or unsanitized values to the Handlebars layout?

How can I pass values to handlebars that are not sanitized upon output? For instance, I want to ensure that when using the code res.render('index', {script: '<script src="bundle.js"></script>'}), it is not rendered as {{scri ...

Javascript downloadable content available for download

<div class="center"data-role="content" id="content" > <a href="#flappy" data-transition="slide" >Avoid Becoming a Terrorist</a> </div> <div id="flappy" > <center> <div id="page"> ...

Issue arised while trying to open Bootstrap modal window due to conflict with surrounding elements

I'm facing a challenge with getting the bootstrap modal window to pop up on my website. Despite trying various solutions and troubleshooting steps, I haven't been able to resolve the issue. Even after eliminating all scripts except for the boots ...

What is the reason for the failure of this code to store data in local storage (undefined)?

After creating a new div with data from an input form, I noticed that the first div properly saves the inputted data. However, when I try to input new data, the div displays an undefined value. first attempt second attempt 0: {name: "Milk", amount: "30"} ...

Buttons aligned vertically alongside an input text field

I am trying to align two buttons vertically under an input text box with the middle aligned. This is what I have done so far: jQuery(document).ready(function($) { // Implementing bootstrap minus and plus plugin // Reference: http://jsfiddle.net/lael ...

Using JavaScript to Render an Array in HTML through Looping (with JSON)

I am facing an issue where only the first value of my associative array is being returned when I loop through it in my HTML file. However, when I check it using console.log, all data is displayed correctly. There is no error displayed in the HTML, but not ...

Utilizing JQuery to merge variable identifiers

Imagine a scenario where the variables are structured this way: localStorage.var1a = 0; localStorage.varnum = 1000; Now, consider the following code snippet: for(x = 1; x < localStorage.varnum; x++){ if(localStorage.var.x.a > 0){ localStora ...

Rails and Javascript: Partial does not retrieve data attribute, yet it is visible in the DOM

Trying to show the select field value for shipping costs below the field. Thanks to @Vasfed, using data attributes and JavaScript. However, the data attribute is visible in the DOM when the page loads but not in the returned data for the AJAX call when che ...

Display JavaScript and CSS code within an Angular application

I am working on an Angular 1.x application (using ES5) where I need to display formatted CSS and JS code for the user to copy and paste into their own HTML file. The code should include proper indentations, line-breaks, etc. Here is a sample of the code: ...

When errors occur while printing HTML through an Ajax request, it can hinder the functionality of other JavaScript code

I recently conducted an interesting experiment on my website. The concept involved sending an AJAX request to a PHP file, which then retrieved a random website by using various random words for search queries on Google. The retrieved website content was th ...

Is it possible to save data to a file using React?

As I work on a React web application, the need has arisen to store crucial user data on the client side in a stable manner. Due to the requirement of data stability and the constraint against using Indexed DB, I am considering storing the data as JSON in ...

Preserving hyperlinks while transferring text from an HTML element using JavaScript

I'm struggling to preserve links from HTML elements while copying rich text, but have been unable to achieve it despite numerous attempts. The following code represents my best effort so far, however it only copies text without maintaining the links ...

When JSON data is copied and displayed in Node.js or JavaScript, it appears as a string

I have written a code to copy one JSON file into another JSON file. Here is the code: function userData(){ fs.copyFile( path.join(process.cwd(), "user/access.json"), path.join(process.cwd(), "client/access.json"), ...

Ways to conceal sidebar after user logs out and reveal sidebar upon user's successful login?

Currently, I am utilizing the Dashgum bootstrap template obtained from Gridgum for a specific assignment. The task involves displaying the side navbar for logged-in users persistently, even after they have closed the site, and hiding the side navbar comple ...

Sort activities according to the preferences of the user

This is the concept behind my current design. Click here to view the design When making a GET request, I retrieve data from a MongoDB database and display it on the view. Now, I aim to implement a filter functionality based on user input through a form. ...

The use of JavaScript within the code results in the <header> and <nav> elements failing to display on the browser

Currently, I am tackling a webpage project that involves using scripts in both the head and section elements to generate a table where clicking on the cells changes their color randomly. However, a glitch seems to be hindering the display of the header and ...

Form fields in Bootstrap 4 will consistently expand downward in the select dropdown menu

When using bootstrap 4, I want my field to always expand downwards and never upwards, even when half the screen is taken up by the form's select element. How can I achieve this effect? I have tried using data-dropup-auto="false" but it didn't wo ...

Determining the Presence of a Value in an Array using .includes

I've been facing challenges trying to use .includes with my array. My goal is to have a function that checks if a value already exists and removes it from the array if it does. The value is a string obtained from an object with a .name property. 0: { ...

Converting a JSON object into an array of objects

I am looking to transform the following JSON structure let data = { item1: [11, 12, 13, 14, 15], item2: [16, 17, 18, 19, 20] } into this specific format using JavaScript's native functionalities of arrays or objects (compatible with all web brow ...

To enhance the MUI v5 table row, simply apply a border when the mouse hovers

After working on creating a table with Material UI v5, I encountered an issue where I wanted to add a blue border to the table row when the mouse pointer hovered over it. I attempted several methods and did thorough research, but unfortunately, I was unab ...