Tips for keeping polyline layer at the forefront?

I am working with two geojson files containing polygons, each assigned to a different radio button. Additionally, there is a checkbox that contains a geojson polyline. My issue is that when I switch between the polygon layers, the polyline layer gets hidden. Is there a way to ensure that the polyline layer always stays visible in the front using BringToFront() or any other method?

You can view the simplified project on jsfiddle

http://jsfiddle.net/albertovich/fe0vtytb/

var poligon1 = new L.LayerGroup();
var poligon2 = new L.LayerGroup();
var polyline1 = new L.LayerGroup();
var map = L.map('map', {
  center: [39.4699075, -0.382881],
  zoom: 13,
  layers: [poligon1]
});

var baseLayers = {
  "Poligon1": poligon1,
  "Poligon2": poligon2
};

var myStylePol1 = {
  "weight": 2,
  "opacity": 1,
  "color": "#FF00CC",
  "fillOpacity": 1
};

var myStylePol2 = {
  "weight": 2,
  "opacity": 1,
  "color": "#FFFF80",
  "fillOpacity": 1
};

var myStyle3 = {
  "weight": 2,
  "opacity": 1,
  "color": "#000"
};

L.geoJson(poli1, {
    style: myStylePol1
  }).addTo(poligon1),
  L.geoJson(poli2, {
    style: myStylePol2
  }).addTo(poligon2),
  L.geoJson(polyl3, {
    style: myStyle3
  }).addTo(polyline1);

var overlays = {
  "Polyline": polyline1
};

L.control.layers(baseLayers, overlays).addTo(map);

Answer №1

If you're looking to achieve a similar effect, you can use the following code snippet:

map.on('baselayerchange',function(baselayer){ 
  baselayer.layer.getLayers()[0].bringToBack(); 
});

Check out the updated Fiddle here

Keep in mind that baseLayers are typically tile layers like streets, satellite, and terrain, rather than geoJson polygons. Consider using simple overlays for all your layers.

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 could be causing the background image on my element to not update?

I'm attempting to utilize a single background image that is 3 pixels wide and 89 pixels tall. Each vertical stripe of 1 pixel will serve as the background for a different div. I had presumed that adjusting the background-position by -1px 0 and specif ...

What is the best way to showcase the reading from a stopwatch on my screen?

Working on these codes has been quite a challenge for me. I have recently embarked on a JavaScript journey as a beginner and attempted to create a stopwatch project. However, it seems like I may have missed the mark with the logic or implementation somewhe ...

Generate your API with the NODEJS Express application generator

The current functionality of the Express JS Express application generator allows for the generation of an initial App, also known as a Boilerplate, using the npx express-generator or express myapp commands depending on the version of Express. The default s ...

Interact with Click Events and Choice Values

I am facing two issues which I need help with. The first one involves hiding and showing listboxes based on the selected radio button. The second issue is related to checking if listbox options contain null values or empty spaces and removing them. Unfortu ...

Tips for accessing an Element by its Id in Vue

I am trying to access an external Element in Vue by using the method getElementById(): new Vue({ el: "#vue", data: { helloElement: document.getElementById('hello') } }) <script src="https://cdnjs.cloudflare.com/ajax/libs/v ...

Converting a JavaScript IIFE library into a React Component

Hello, I am currently in the process of learning React JS and there are two JavaScript files that I am working on: Polyfill.js -> hosted on github CustomNavbar.js -> created by me Here is the structure of polyfill.js: export default (function(win ...

Execute the button click function repeatedly at specific time intervals using Javascript

I have a submit button on my page that uses the POST method to send a value. <button id="log" class="btn bg-purple" formmethod=post value="2" name="start" formaction=start_server.php>Get Log</button> Clicking this button retrieves a log file ...

(Is it even necessary to use a timezone library for this straightforward scenario?)

As I delve into the realm of time zones for the first time, I've heard tales of how challenging it can be for developers. To ensure I am on the right track, I am posing this question as a safeguard to make sure nothing is overlooked. My scenario is q ...

Issue with mouseMove function not aligning correctly with object-fit:contain CSS property

My current code allows users to select a color from an image when hovering over the pixel with the mouse. However, I am encountering an issue where the colors do not map correctly when using object-fit: contain for the image. The script seems to be treatin ...

Enhance your Vue app with filtered categories using Vue-google-chart

What is the process for creating a filter category in a bar chart using the vue-google-charts wrapper in Vue.js? I have successfully created a stacked bar chart and now I am looking to add a filter by label inside the legend. Here is my vue application: ...

Printing a modified div element that was created using jQuery on an ASP.NET MVC webpage after the initial page has finished loading

In my view, there is a div that holds multiple tables generated based on the model. Each row in these tables contains fields that can be edited using TextBoxFor. Through jQuery, rows can be moved between tables and new tables can be created dynamically wit ...

What is the process for assigning variables to modules using RequireJS?

Is there a way to define variables for modules in RequireJS? In simpler terms, how can I achieve the equivalent of the following using RequireJS: var fs = require('fs'); var child_process = require('child_process'); I am looking to s ...

When is it appropriate to utilize a view in EmberJS?

As a new user of EmberJS, I am navigating through the API and core components of the framework. One aspect that is challenging for me to fully comprehend is knowing when to utilize an Ember.View as opposed to an Ember.Component. My current understanding is ...

What could be causing my if-else statement to malfunction in JavaScript?

I'm working on a form where certain conditions need to be met based on the selected order type. For instance, if a market order is chosen, the stop price and limit price should default to zero and become read-only fields. Similarly, selecting a limit ...

Monitor the automatic redrawing system of mithril.js

I am considering using mithril.js to develop a high-performance application that will involve managing thousands of DOM elements that may need frequent updates. It is crucial for me to ensure that only the necessary elements are redrawn when changes occur. ...

The Next JS build process exclusively creates server-side pages

I have noticed that some of my pages like /contact, /about, and /rules are server-side generated even though I am not using getServerSideProps(). Since these pages only contain static images and text without any API requests, I want them to be treated as s ...

I am encountering an issue where the msal-browser login process seems to be frozen at the callback

After successfully installing the msal-browser package, I am able to log in. However, I encounter an issue where the screen gets stuck at the callback URL with a code. The samples provided in the GitHub repository demonstrate returning an access token in ...

What is the significance of using index 0 for caching an ID in jquery?

What is the reason behind using an index of 0 in the following code? var $one = $('#one')[0]; Is there a specific purpose for not just using var $one = $('#one'); ? SOURCE I came across the above code while researching about jQuery, ...

Incorporating tawk.to into a Nuxt/Vue application

Has anyone had success implementing tawk.to in a Nuxt application? I took the initiative to create a file called "tawk.js" in my plugin folder and added the following code: var Tawk_API = Tawk_API || {}, Tawk_LoadStart = new Date() (function () { ...

What are some best practices for integrating jointjs with reactjs?

Could the jointjs library be utilized alongside reactjs? Are there any libraries or wrappers that facilitate this integration? If not, what is the most straightforward method to incorporate it? ...