Discover the process of efficiently loading multiple markers in Leaflet by integrating VueJs

I created a web application that showcases various markers on a map, and these markers are loaded dynamically as the user interacts with the map. I make an API call based on the current bounds of the map to retrieve a list of markers.

The existing approach

<l-map v-if="showMap" :zoom="zoom" :center="center" @update:bounds="boundsUpdated" style="height: 100%">
    <l-tile-layer :url="url" :attribution="attribution"/>
    <l-marker v-for="marker in markers" :key="marker.id" :lat-lng="marker.coordinates" >
      <l-icon
          icon-url="https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-green.png">
      </l-icon>
    </l-marker>
</l-map>

...

mounted() {
    let thisClass = this;
    EventBus.$on('new_markers', function (payLoad) {
      for (const marker of payLoad) {
        thisClass.markerMap[marker.id] = marker;
      }
      thisClass.markers = Object.values(thisClass.markerMap)
    });
  },

Given the slow rendering speed of all markers and the overheating issue on my laptop, it appears that the system is re-rendering all markers each time.

My query

Is there a way to instruct leaflet to only load new markers from the updated list instead of reloading the entire list every time?

Answer №1

It appears that the issue lies in rendering all the markers each time
-> Have you attempted to log markers to determine the number of items present?

If your goal is to only display newly added markers on the map from the markers array, you can modify

v-for="marker in markers"
to
v-for="marker in lastMarkers
, where lastMakers is a computed property that returns only the latest items like this:

lastMarkers() {
 return this.markers.slice(this.lastMarkerIndex);
}

You will need to update lastMarkerIndex in your EventBus callback function just before assigning

Object.values(thisClass.markerMap)
to thisClass.markers

To eliminate the use of thisClass and substitute it with this, you can utilize an arrow function for your callback instead of function(payload)

In summary, your EventBus listener could be structured as follows:

EventBus.$on('new_markers', (payload) => {
  for (const marker of payload) {
    this.markerMap[marker.id] = marker;
  }
  this.lastMarkerIndex = this.markers.length - 1;
  this.markers = Object.values(this.markerMap)
});

This approach illustrates how I would handle the situation, but there are certainly various methods to achieve your desired outcome.

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

Managing the jQuery.noConflict function

Upon review, I noticed that the scripts I inherited start like this: var $j = jQuery.noConflict(); The purpose behind this code is not clear to me. While I understand the intent is to avoid conflicts, I am uncertain about the specific conflict it aims to ...

Change the background color of a webpage by clicking with the help of Javascript

I've been experimenting with a script that changes the CSS background color on click, but I'm having trouble adding a third color option. Can anyone provide guidance on how to make the third color work? Check out my jsfiddle here: https://jsfid ...

Exploring visible faces of three-dimensional objects using Three.js camera frame

Searching for a way to identify the visible faces of a Mesh with respect to a PerspectiveCamera. I am not concerned about obscured or unobscured faces, just those that fall within or outside the camera's view. For instance, when observing a sphere and ...

The issue of undefined return for multiple columns in MVC when using MCAutocomplete Jquery UI

My MVC multiple column is returning undefined. What am I missing or what is wrong with my code? Please help. https://i.sstatic.net/euwe8.png Controller public ActionResult EmployeeIDSearch(string term) { // Get Tags from data ...

Increase the identifier of my input text when a table row is duplicated through Javascript

I have a table with only one row that contains various elements like textboxes and buttons. I want to clone the table row using the append() function when a button is clicked. However, I am facing an issue - how can I increment the id of the textboxes and ...

Is it possible to use a += operator with attr() and val() functions in JavaScript?

Ever since I switched to jQuery, my development process has significantly sped up. However, there is one task that has become a bit more time-consuming: appending a string to an attribute or value. For instance, if you wanted to add a letter to the value ...

Having trouble utilizing Reactjs Pagination to navigate through the data

I'm currently working on implementing pagination for a list of 50 records, but I'm encountering an issue. Even though I have the code below, it only displays 10 records and I'm unaware of how to show the next set of 10 records until all 50 a ...

Visualizing Data with Flot.js - A Comprehensive Guide

Does anyone know how to organize values in a flot histogram by day, merging them into one bar per day? I've been searching for a solution but can't seem to find one. __ If you have any ideas, please share! ...

I am interested in creating a checkbox with a radio button style in VueJs

I am looking to create a radio-style checkbox in VueJs. Check out the code I've already put together at this link: https://jsfiddle.net/WeiChienHsing/w4nxhLg2/ Given my limited experience with VueJs, I'm curious if there's a more straightf ...

Creating dynamic bootstrap carousels with the power of jquery and javascript

I need assistance with creating a website using only html, js, and bootstrap. The issue I am facing involves implementing multiple carousels dynamically onload. Below is an example of the girls_ethnic data set. Similarly, I have boys_shirts, boys_pants, ...

Communication between the Node development server and the Spring Boot application was hindered by a Cross-Origin Request

Here is the breakdown of my current setup: Backend: Utilizing Spring Boot (Java) with an endpoint at :8088 Frontend: Running Vue on a Node development server exposed at :8080 On the frontend, I have reconfigured axios in a file named http-common.js to s ...

Special vue directive that displays content only if it exists

Often, I find myself needing to display a div (or another element) only if it contains content. This requires duplicating the reference to the content in the tag and in v-if, as shown below... <div v-if="store.sometimesIWillBeEmpty">{{store.sometime ...

Is there a way to create a self-contained installation package for my Vue application?

Is it possible for my application to be downloaded and installed without requiring internet access after the download is complete? I am looking to create a standalone installer for this purpose. Are there any suggestions on how to go about implementing t ...

bing translator API: the variable text is translated with no content

I'm encountering an issue while working with javascript and PHP. The PHP code seems to run fine until it reaches the $curlResponse variable. From there onwards, all the subsequent variables ($xmlObj, $translatedStr, $translatedText) appear to be empty ...

Encountering a problem with npm reading dependencies

I decided to kickstart a new Node application by following a tutorial and creating a package.json file. Below is the content of my json file: { "name": "Dashboard", "version": "0.0.0", "description": "Client-A Dashboard", "dependencies": { ...

The React Native ListView fails to refresh after sorting or when new data is retrieved

Working on a react native application with a sortable ListView, where data is fetched from JSON and updated periodically. However, facing issues as the list does not update in either scenario. Even after verifying that the array matches expectations befor ...

Use JavaScript to jumble up each individual letter

Is there a way to scramble words letter by letter instead of the whole word at once in Vue.js? I'm new to Vue.js and struggling to make this change in my code. I'm currently using Vue.js for this task. const sampleText1 = 'インバウント ...

Problem with exporting data from the API to a JavaScript file in Excel format

Instead of receiving actual data in the response, I am getting a set of characters. However, everything works fine when I click on Download file in swagger. Can someone help me diagnose the issue? function downloadDocFile(data: Blob, ext = 'xlsx' ...

Clearing the input file: e.stopPropagation does not exist as a function

I found this awesome function on Stack Overflow to reset a file input element. <input type="file" id="image-file" /> Here is the JavaScript code: function clearFileInput(e) { e.wrap('<form>').closest('form').get(0).re ...

Webpack's equivalent to JSPM's override functionality

I am currently working on a project where I have specified an override for a package inside the jspm block in the package.json file. The content of the override block is as follows: "overrides": { "github:repoName/myPackage@master": { "main": "js/so ...