javascript - How to add markers to different layers in leaflet using onEachFeature and geojson - Stack Overflow

My current code merely binds markers according to the data from this json file:{"type": &quo

My current code merely binds markers according to the data from this json file:

{
  "type": "FeatureCollection",
  "features": [
    {
      "geometry": {
        "type": "Point",
        "coordinates": [53.8460456, -38.9135742]
      },
      "type": "Feature",
      "properties": {
        "name": "red"
      }
    }
  ]
}

and my code looks something like this:

const blueLayer = new L.LayerGroup();
const redLayer = new L.LayerGroup();
const link = './data/events2.json' //file above
const map = L.map("mapid", {
  center: [53.8460456, -38.9135742],
  zoom: 12,
  layers: [blueLayer, redLayer],
});

L.tileLayer(
  "/......",
  {
    attribution: "......",
    maxZoom: 18,
    minZoom: 1,
  }
).addTo(map);

$.getJSON(link, (events) => {
  L.geoJSON(events, {
    style: (feature) => (feature.properties && feature.properties.style),
    onEachFeature: onEachFeature,
    pointToLayer: (feature, latlng) => marker(latlng),
  });
}).addTo(map);

However, I would like to add markers to different layers instead of binding them all directly to the map so I can do something like this:

onEachFeature(feature, layer) {
  if (feature.properties.name === "red") {
    //do something that binds associated marker to red layer)
  } else {
   //do something that binds associated marker to bluelayer
  }
}

My current code merely binds markers according to the data from this json file:

{
  "type": "FeatureCollection",
  "features": [
    {
      "geometry": {
        "type": "Point",
        "coordinates": [53.8460456, -38.9135742]
      },
      "type": "Feature",
      "properties": {
        "name": "red"
      }
    }
  ]
}

and my code looks something like this:

const blueLayer = new L.LayerGroup();
const redLayer = new L.LayerGroup();
const link = './data/events2.json' //file above
const map = L.map("mapid", {
  center: [53.8460456, -38.9135742],
  zoom: 12,
  layers: [blueLayer, redLayer],
});

L.tileLayer(
  "https://api.mapbox./......",
  {
    attribution: "......",
    maxZoom: 18,
    minZoom: 1,
  }
).addTo(map);

$.getJSON(link, (events) => {
  L.geoJSON(events, {
    style: (feature) => (feature.properties && feature.properties.style),
    onEachFeature: onEachFeature,
    pointToLayer: (feature, latlng) => marker(latlng),
  });
}).addTo(map);

However, I would like to add markers to different layers instead of binding them all directly to the map so I can do something like this:

onEachFeature(feature, layer) {
  if (feature.properties.name === "red") {
    //do something that binds associated marker to red layer)
  } else {
   //do something that binds associated marker to bluelayer
  }
}
Share Improve this question edited Dec 15, 2023 at 17:36 Mike 'Pomax' Kamermans 53.8k17 gold badges125 silver badges175 bronze badges asked Jun 7, 2017 at 20:35 hebaheba 1191 gold badge2 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I believe you are nearly there. In your onEachFeature function, you can simply add layers to your different Layer Groups accordingly. Then, you can add the Layer Groups to the map whenever you want. Try this out:

function onEachFeature(feature, layer) {
  if (feature.properties.name === "red") {
    // add only 'red' markers to Layer Group
    redLayer.addLayer(layer);
  } else {
    // add only 'blue' markers to Layer Group (assuming just red/blue markers)
    blueLayer.addLayer(layer);
  }
}

Now somewhere else in your code you can add these layers to your map:

redLayer.addTo(map);
blueLayer.addTo(map);

Just make sure to remove the .addTo() method after the L.geoJSON() unless you want all the markers on the map initially. Hope this helps!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742312803a4420236.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信