通过传单中的选项值创建图层组

Create layerGroups by option value in leaflet

我有一个 JS 标记数组,想根据图标值创建 layerGroup 对象。

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

按行排列的 arrayMarkers 内容(按图标排序的标记):

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’})

目标: arrayLayerGroup = [layer1, layer2, layer3];

arrayLayerGroup 内容:

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

我试过这段代码,optionIcon 按预期带回 'http://...icon_A.png',但不知道如何比较同一数组中的值。我无法逐行比较,因为坐标总是不同的:

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++;
    }
} 

例如使用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));