2

The console show this error: "spots is not defined" How can I use my layerGroup spots outside of the function?

$.get("./data.csv", function (csvString) {
  var data = Papa.parse(csvString, { header: true, dynamicTyping: true }).data;

  for (var i in data) {
    var row = data[i];

    var marker = L.marker([row.Latitude, row.Longitude], {
      opacity: 1,
    }).bindPopup(row.Name);

    var spots = L.layerGroup([marker]);
  }
});

var overlaySpots = {
  "Some name": spots,
};

L.control.layers(null, overlaySpots).addTo(map);

spots.addTo(map);

1 Answer 1

1

Define the layergroup outside and then add the marker:

var spots = L.layerGroup();

$.get("./data.csv", function (csvString) {
  var data = Papa.parse(csvString, { header: true, dynamicTyping: true }).data;

  for (var i in data) {
    var row = data[i];

    var marker = L.marker([row.Latitude, row.Longitude], {
      opacity: 1,
    }).bindPopup(row.Name);

    spots.addLayer(marker);
  }
});

var overlaySpots = {
  "Some name": spots,
};

L.control.layers(null, overlaySpots).addTo(map);

spots.addTo(map);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.