简体   繁体   English

用于从对象数组创建数组的 Vanilla Javascript

[英]Vanilla Javascript for creating an array from an array of objects

var cityMarkers = [
    {
        id: "bliss",
        name: "Principality of Bliss",
        icon: cityIcon,
        coords: [-90.19, -76.90]
    },
    {
        id: "cantonia",
        name: "Grand City of Cantonia",
        icon: cityIcon,
        coords: [-39.513421, -69.09375]
    },
    {
        id: "mithril",
        name: "Grand City of Mithril ",
        icon: cityIcon,
        coords: [42, -102.5]
    }];

I have the above in a separate file for referencing from my app.js file.我将上述内容放在一个单独的文件中,以便从我的 app.js 文件中引用。

  cityMarkers.forEach(function(item) {
      var marker = L.marker(item.coords, {icon : item.icon});
      marker.bindTooltip("<b>" + item.name + "<b>", {permanent: true, offset: 
  [60, 0]});

This will make the markers and the other properties, but it won't put them on the map.这将制作标记和其他属性,但不会将它们放在地图上。 An array handles placing them on the map, so this doesn't help me much with what I am really trying to do.一个数组处理将它们放置在地图上,所以这对我真正想做的事情没有多大帮助。

This is a map based on the leaflet library.这是基于传单库的地图。 I am trying to assign a variable to each city with the id.我试图用 id 为每个城市分配一个变量。 Then, after the markers are made and attached to their variables, I want to make an array out of those names to function as the data layer.然后,在创建标记并将其附加到它们的变量后,我想用这些名称创建一个数组以用作数据层。 I admit that I am out of my depth, here.我承认我的深度不够,在这里。 Any guidance would be most appreciated.任何指导将不胜感激。 I linked the documentation below in case anyone wants it.如果有人需要,我链接了下面的文档。

https://leafletjs.com/reference-1.3.4.html https://leafletjs.com/reference-1.3.4.html

I did research the question, but I was unable to find any results that answered what I think I am asking.我确实研究了这个问题,但我找不到任何可以回答我认为我要问的问题的结果。 I would highly prefer a nudge over a flat answer.我更喜欢轻推而不是平淡的答案。 I don't understand how to instantiate the variables and bind them to the markers.我不明白如何实例化变量并将它们绑定到标记。 Thank you for your time.感谢您的时间。

Instead of adding the markers directly to the map, add them to a L.layerGroup .不要将标记直接添加到地图,而是将它们添加到L.layerGroup You can add the layerGroup to the map and remove it again at will.您可以将layerGroup添加到地图中,然后layerGroup再次删除它。

var lg = new L.layerGroup();
cityMarkers.forEach(function(item) {
      var marker = L.marker(item.coords, {icon : item.icon});
      marker.bindTooltip("<b>" + item.name + "<b>", {permanent: true, offset: 
  [60, 0]})
  .addTo(lg)});

lg.addTo(map);       // Add the layerGroup the map
lg.removeFrom(map);  // Remove the layerGroup from the map

I think you can try adding .addTo(map)我想你可以尝试添加.addTo(map)

cityMarkers.forEach(function(item) {
  var marker = L.marker(item.coords, {icon : item.icon});
  marker
    .bindTooltip("<b>" + item.name + "<b>", {permanent: true, offset: [60, 0]})
    .addTo(map);

Demo for adding multiple markers to leaflet.向传单添加多个标记的演示。

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

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