admin管理员组文章数量:1333431
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
1 Answer
Reset to default 3I 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!
版权声明:本文标题:javascript - How to add markers to different layers in leaflet using onEachFeature and geojson - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742312803a2451214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论