Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
Create layerGroups by option value in leaflet

I have a JS array of markers and would like to create layerGroup objects based on icon value.

arrayMarkers = [marker1, marker2, marker3, marker4, marker5, marker6];

arrayMarkers content by line (markers ordered by icon):

marker1 = L.marker ([4.8,44.7], {icon :’http://…icon_A.png’})
marker2 = L.marker ([4.1,44.2], {icon :’http://…icon_A.png’})
marker3 = L.marker ([4.5,44.9], {icon :’http://…icon_B.png’})
marker4 = L.marker ([4.2,44.5], {icon :’http://…icon_B.png’})
marker5 = L.marker ([4.2,44.5], {icon :’http://…icon_B.png’})
marker6 = L.marker ([4.9,44.7], {icon :’http://…icon_C.png’})

Goal : arrayLayerGroup = [layer1, layer2, layer3];

arrayLayerGroup content:

layer1 = L.layerGroup ([marker1, marker2])
layer2 = L.layerGroup ([marker3, marker4, marker5])
layer3 = L.layerGroup ([marker6])

I have tried this code, optionIcon brings back 'http://...icon_A.png' as expected, but have no clue how to compare values from same array. I can't compare line by line as coordinates are always different:

var j=1;
for (i=0; i<arrayMarkers.length; i++){          
var optionIcone = arrayMarkers[i].options.icon.options.iconUrl;
var optionIcone2 = arrayMarkers[j].options.icon.options.iconUrl;
    if (optionIcone !== optionIcone2){
        var layer = L.layerGroup([arrayMarkers[i]]);
        arrayLayerGroup.push(layer);
        j++;
    }
} 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For example using Lodash groupBy:

// Group the Markers by iconUrl
var markersGroupedByIconUrl = _.groupBy(arrayMarkers, "options.icon.options.iconUrl");

// Convert each group of Markers which have same iconUrl into a Layer Group
var arrayLayerGroup = Object.values(markersGroupedByIconUrl)
  .map((listOfMarkers) => L.layerGroup(listOfMarkers));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!