1

I'm trying to add a value to an L.imageOverlay in leaflet. When you add the layer on the layer control panel, it adds the value using the function below to the total, I've tried it with an attribution, but this doesn't seem to work either. Is it possible to add data to the L.imageOverlay?

var EXVOORh01 = L.imageOverlay("EXVOORh.png", bounds, value="400000", onchange=totalIt());

  function totalIt() {
    var input = document.getElementsByClassName("leaflet-control-layers-selector");
    var total = 0;
    for (var i = 0; i < input.length; i++) {
      if (input[i].checked) {
        total += parseFloat(input[i].value);
      }
    }
    document.getElementsByName("total").value = "A" + total.toFixed(1);
  }
0

1 Answer 1

2

You cannot assign HTML attributes to L.imageOverlay object like you are try to do, but you can 'misuse' layer object for attaching value to layer. Since layer is standard JS object, you can assign any property to it and use it later.

In your case this could look something like this:

var EXVOORh01 = L.imageOverlay("EXVOORh.png", bounds);
EXVOORh01.myValue = 40000;

Name of the property can be anyting, as long is it's not in conflict with standard propterties.

4
  • Thank you! Is it possible to add the onchange= totalIt()) to the L.imageOverlay directly?
    – GladH
    Commented Apr 27, 2020 at 13:01
  • 1
    Answer to your comment is: No. Answer above is answering your question "Is it possible to add data to the L.imageOverlay?" And that was the only question. If answer helped you in the right direction, it's custom to accept it or at least upvote it. If you have another question, please post it as separate question.
    – TomazicM
    Commented Apr 27, 2020 at 14:43
  • Oke, i can't upvote yet because I don't have 15 reputation yet.
    – GladH
    Commented Apr 27, 2020 at 14:51
  • 2
    To dynamically sum values assigned to displayed overlays, you have to listen to map overlayadd and overlayremove events and then iterate through overlays with map eachLayer method.
    – TomazicM
    Commented Apr 27, 2020 at 15:39

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.