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

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);
  • Thanks a lot Falke! – SM21 Jan 13 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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