简体   繁体   English

如何循环遍历坐标数组以创建layerGroup的地图标记?

[英]How to loop through an array of coordinates to create map markers for a layerGroup?

Here's a quick snippet of what it looks like currently -- using the for loop. 这是目前看起来像的一个快速片段 - 使用for循环。 I only took a piece of the map to use an example: 我只拿了一块地图来使用一个例子:

link 链接

The picture is simply showing that, as of right now, my code automatically displays all of the fire extinguishers as soon as the map loads -- which is not what I want. 图片显示,截至目前,我的代码会在地图加载后自动显示所有灭火器 - 这不是我想要的。 Please read below for a rather detailed version of my question. 请阅读下面的我的问题的相当详细的版本。

    // Fifth Grade class teachers //

    var blower = L.marker([-38.68551, -44.12109], {icon: fiveTeacherIcon}).bindPopup('Mrs. Blower\'s class'),
        okonowski = L.marker([-55.47885, -43.41797], {icon: fiveTeacherIcon}).bindPopup('Mrs. Okonowski\'s class'),
        vermeulen = L.marker([-38.95941, -23.37891], {icon: fiveTeacherIcon}).bindPopup('Mrs. Vermeulen\'s class');

var fifthGrade = L.layerGroup([blower, okonowski, vermeulen]);

var classesOverlay = {
    "Kindergarten": kindergarten,
    "First Grade": firstGrade,
    "Second Grade": secondGrade,
    "Third Grade": thirdGrade,
    "Fourth Grade": fourthGrade,
    "Fifth Grade": fifthGrade
};

L.control.layers(classesOverlay).addTo(cmap);

As you can see above, this is how I display the markers for the fifth grade classes. 如上所示,这就是我如何显示五年级的标记。 When I click on "Fifth Grade" on the layer selection option (while viewing the map) those three markers pop-up. 当我在图层选择选项上单击“五年级”时(在查看地图时)会弹出这三个标记。 This is exactly what I want for my fire extinguishers. 这正是我想要的灭火器。

But I want a more efficient way to do so due to the fact that I have so many coordinates. 但是由于我有这么多坐标,我想要一种更有效的方法。 Whereas for Fifth Grade I only have three, so I didn't mind manually entering them individually. 而五年级我只有三个,所以我不介意单独手动输入。

So my question was basically asking if there's a more efficient way to do this. 所以我的问题基本上是询问是否有更有效的方法来做到这一点。 As I showed above, I currently have it run a for loop through the array and place them onto the map, but the problem is that I don't want the fire extinguishers to be showing the whole time; 正如我在上面所示,我现在让它在数组中运行for循环并将它们放在地图上,但问题是我不希望灭火器显示整个时间; I want them to be just like I have Fifth Grade (and obviously the other grades too), where I simply select them from the layerGroups and then they all appear. 我希望它们就像我有五年级(显然也是其他等级),我只是从layerGroups中选择它们然后它们都出现了。

I just don't want to have to individually make a marker for every set of coordinates for the fire extinguishers, because I feel like that is really counter-productive. 我只是不想为灭火器的每一组坐标单独制作一个标记,因为我觉得这确实适得其反。

I can't figure out how to make it so when selected it will run the for loop and display all of them; 我无法弄清楚如何选择它,所以选择它将运行for循环并显示所有这些; it only ended up displaying one set of coordinates when I attempted. 我尝试时只会显示一组坐标。

I hope this made more sense. 我希望这更有意义。 I really appreciate you taking the time out of your day to help me! 我非常感谢你抽出时间来帮助我!

function createMarker (coords) {

    return (new L.marker(coords, {icon: fireEx}).bindPopup('<strong>Hi! I\'m a fire extinguisher! And you are?</strong>').addTo(cmap));
}

function createMarkerGroup (markerLocationList) {
    var
        markerList = [];

    markerLocationList.forEach(function (location) {

        markerList.push(createMarker(location));
    });

    return L.layerGroup.apply(L.layerGroup, markerList);
}


var extinguishers = [
    [71.52491, -17.75391],
    [71.69129, 0.35156],
    [60.84491, -56.25],
    [49.49667, -14.41406],
    [10.66061, -33.75],
    [11.3508, -10.01953],
    [-21.45307, -22.5],
    [-50.1769, -60.46875],
    [-49.49667, -27.94922],
    [-59.88894, 36.03516],
    [-48.80686, 47.10938],
    [-49.15297, 84.72656],
    [-49.15297, 84.72656],
    [-48.80686, 47.10938]
];
var fireExtinguishers = createMarkerGroup(extinguishers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM