Removing Arcs from a Map in Leaflet

On my map, I use arc.js to draw trade routes between countries as arcs. Each arc represents a different year, and I want to be able to update them based on the selected year.

This is how I create the arcs:

// Draw arcs for each trade route
var line_to_add;
for(var q = 0; q < dest_Coor.length; q++) {
    var start = {x: from_Coor[0][0], y: from_Coor[0][1]};
    var end = {x: dest_Coor[q][0], y: dest_Coor[q][1]};
    var generator = new arc.GreatCircle(start,end);
    var line = generator.Arc(100, {offset: 10});

    line_to_add = L.geoJson(line.json(), {
        color: "#004c00"
    });

    allArrows.push(line_to_add);
    line_to_add.addTo(map);
}

When a different year is chosen, I attempt to delete the current arcs like this:

function deleteOldArcs() {
    for(var i = 0; i < allArrows.length; i++) {
        map.removeLayer(allArrows[u]);
    }
    allArrows.length = 0;
}

However, the program freezes when trying to remove the layers using map.removeLayer(allArrows[u]). I have confirmed that the arc objects are in map._layers, so why does map.removelayer not work?

I would appreciate any assistance with this issue. Thank you!

Answer №1

Here are a few different options to consider:

  1. Take advantage of Leaflet.Arc, which simplifies the process by creating Leaflet layers directly, providing a more organized approach compared to managing json blobs.

  2. Save the `line_to_add` in an array for future reference, allowing you to easily call its `remove()` method later on. It seems like you intend to use `allArrows[u]`, but you must first populate the `allArrows` data structure.

  3. Utilize a L.LayerGroup. Simply add the `LayerGroup` to the map, insert the arcs into the `LayerGroup`, and then utilize the clearLayers() method when necessary.

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

What steps should I take to make sure the vuex store is included in my build process?

I am currently working on a Vue application using vue cli 3. I found a guide here that explains how to build the app with vue-cli-service build --target wc --name my-element [entry] In order to test the output, I have created an index.html file: <!D ...

Transform HTML into a Vue component dynamically during runtime

Currently, I am in the process of learning Vue and working on a simple wiki page project. The raw article data is stored in a JSON file written using custom markup. For example, the following markup: The ''sun'' is {r=black black} shoul ...

Enhance your Angular2 Directive

Looking to customize the angular2-clipboard npm package by extending its functionalities. Specifically, I aim to access and modify its ngOnInit() function to cater to a specific copying use case. Being new to angular2, I am uncertain about the process of ...

How can I generate 200 divs that are identical but each displaying a unique file (page0.svg, page1.svg..., page200.svg)?

<div class="mySlides"> <img src="1173/page0.svg" style="width:50%"> </div> <div class="mySlides"> <img src="1173/page1.svg" style="width:50%"> </div> My task involves generating approximately 200 similar div codes ...

Nested ng-repeat in AngularJS allows you to loop through arrays

I am currently facing an issue with updating the choices in my object that contains a question and list of choices. I'm using nested ng-repeats to display this object on the page. While changing the 'question' works perfectly fine, modifying ...

browsing through a PHP document

I have downloaded files from my SVN repository and saved them on my local disk. These files are mostly PHP files. I am looking for a way to read in documents (other than "txt") located on my local disk and open them on a website that uses PHP. Here is what ...

Halting a function upon meeting a specific condition and resuming it based on another condition

There is a checkbox that acts like a switch button to toggle a functionality ON/OFF. When it is turned ON, the function should work, and when it is turned OFF, the function should not work. However, the issue arises when the switch is turned ON, the fun ...

Transmit analytical data to an external domain without expecting a reply

Initial Conditions I have ownership of mysite.com I lack ownership of othersite.com, but I am able to embed javascript code on that site Query What is the method to transmit analytic data from othersite.com to mysite.com ? ...

Managing promises with recursion in JavaScript: Best practices

I need a solution to go through all the files in a directory recursively and receive confirmation once all files have been processed. The directory structure is outlined below: sample │ app.js └───Temp1 │ │ temp1.js └───Temp2 ...

A guide to customizing node names using vue-slider-component

I am facing an issue with the vue-slider-component. Below is the link to my current test module: template:` <div> <vue-slider v-model="value" :order="false" :tooltip="'always'" :process="false" ...

Retrieving and showing information from a database (Using Javascript Ajax)

I'm in need of assistance with a project for my course and would appreciate any guidance or help that can be offered. My task involves creating a simple JavaScript XML Http Request to display basic information (specifically the country_name & country_ ...

Issue arises in Vue.js component array misalignment upon substituting with a smaller array

My journey with Vue.js (version 2.5.16) has just begun, but I've encountered a puzzling issue related to Reactivity: I have two sets of components displayed using v-for, which mostly render correctly - I can interact with them and even add new ones. ...

Encountered a type error while attempting to render and define table columns within a material table component in

Hey there! I'm currently using the Material table to create a table view, and here are the columns that I have defined: const columns: TableColumn[] = [ { title: 'TYPE', field: 'type_of_action', highligh ...

javascript verify that the input is a valid JSON object

Seeking assistance with an if statement that checks for a json object: updateStudentData = function(addUpdateData) { var rowDataToSave; if(addUpdateData.data.row) { rowDataToSave = addUpdateData.data.row; } else { rowDataToSav ...

Using the express.Router instance for handling errors can be a useful tool in your development

Based on the documentation, it states that any nodejs express middleware function has the capability of being swapped out by App or Router instances: Given that router and app adhere to the middleware interface, they can be used just like any other midd ...

When attempting to access the length of a JSON array, it results in undefined

I recently received a JSON encoded array from an AJAX call that looks like this: {"country":{"0":"United States of America","United States of America":{"states":{"0":"Alaska","Alaska":{"cities":["Adak","Akiachak","Akiak","Akutan","Alakanuk"]}}}}} Below ...

What is causing the crash in p5.js when I use this function to add objects to an array?

I'm currently working on an evolution simulation app that involves reproducing organisms with a health level above 75%. After reproduction, their health is reduced by half. However, I'm encountering an issue where my app crashes in p5.js for reas ...

Exploring various data promises in AngularUI router

I am attempting to utilize the $q service to resolve multiple promises using the $q.all() function with AngularUI router. However, I am encountering issues where it is failing or not functioning as expected. This snippet is from my configuration file that ...

Using Vue.js to loop through and display a set of images from the assets directory using v-for

I have a collection of 100 images from 1.png to 100.png stored in my assets folder. I am looking to dynamically display them using v-for without having to manually specify each individual image URL. <div v-for="n in 100"> <img :src="`../asset ...

The anchor is no longer functioning

Having just started with Javascript, I am facing some issues on my website... I have implemented the fullPage.js script along with another script for a vertical menu containing hidden submenus. However, the script for the navigation menu is not functioning ...