TransWikia.com

Leaflet - defining layer features for group of layers

Geographic Information Systems Asked on August 4, 2021

I would like to define the specified features for more than 1 layer.

I prepared the code like this, but unfortunately it dodn’t work.

 var path = L.layerGroup([ecl, ant, hyb], {
  style: function (feature) {
  return {color: feature.properties.color};
  },
  coordsToLatLng: function (coords) {
  var latLng = L.CRS.EPSG3857.unproject(L.point(coords)); 
   return latLng;
  }
  }).bindPopup(function (layer) {
   return layer.feature.properties.descriptio;
  }).addTo(map); 

I found a similar approach here:

https://stackoverflow.com/questions/36007088/leaflet-handling-a-geojson-composed-of-multiple-geojson

but it looks like I have to rewrite these properties for every single layer. I am going to have them at least a dozen or so.

The layers used in the code above are already under these links:

https://jsfiddle.net/ao07qjbh/

https://jsfiddle.net/xmdyqvr6/

https://jsfiddle.net/9sj25upo/

Is there any way to bunch them together and define one style?

One Answer

For starter, ecl, ant and hyb ar not layers, but GeoJSON objects.

Second, if you look in the Leaflet docs , L.layerGroup has no such options as style or coordsToLatLng.

The simplest way to achieve what you want is to use L.geoJSON method addData to add individual GeoJSON objects to GeoJSON layer:

var path = L.geoJSON([], {
  style: function (feature) {
    return {color: feature.properties.color};
  },
  coordsToLatLng: function (coords) {
    var latLng = L.CRS.EPSG3857.unproject(L.point(coords));
    return latLng;
  }
}).bindPopup(function (layer) {
  return layer.feature.properties.descriptio;
}).addTo(map);

path.addData(ecl);
path.addData(ant);
path.addData(hyb);

Correct answer by TomazicM on August 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP