1

I made an leafpad openstreetmap with marker and popups. I now want to change a picture displayed in another <div id="plot"> on that website depending on which marker clicked. For updating the div i thought of using jquery. My problem is now to get the content(image url) of the array when I click on a marker... I would appreciate any help.

function add_marker() {
    var points = [
        ["P1", 51.57186, 11.8517, '<img src="img/1.JPG" alt="" width="350px">'],
        ["P2", 51.57886, 11.8117, '<img src="img/2.JPG" alt="" width="350px">'],
        ["P3", 51.57586, 11.8017, '<img src="img/3.JPG" alt="" width="350px">']
    ];

    var marker = [];
    for (var i = 0; i < points.length; i++) {
        marker[i] = new L.Marker([points[i][1], points[i][2]])
        marker[i].addTo(map);
        marker[i].bindPopup(points[i][0]);
        marker[i].on('click',  onClick);
    }

    function onClick(e) {
        $('#plot').html(points[i][3]);
    };
}

2 Answers 2

2

You need to pass along references to points and i as arguments to your onClick function, as they are not within the global namespace.

function add_marker() {
    var points = [
        ["P1", 51.57186, 11.8517, '<img src="img/1.JPG" alt="" width="350px">'],
        ["P2", 51.57886, 11.8117, '<img src="img/2.JPG" alt="" width="350px">'],
        ["P3", 51.57586, 11.8017, '<img src="img/3.JPG" alt="" width="350px">']
    ];
    var marker = [];
    var i;
    for (i = 0; i < points.length; i++) {
        marker[i] = new L.Marker([points[i][1], points[i][2]])
        marker[i].addTo(map);
        marker[i].bindPopup(points[i][0]);
        marker[i].on('click', function(e){
          onClick(e, points, i);
        });
    };
};

function onClick(e, points, i) {

    $('#plot').html(points[i][3]);
};
Sign up to request clarification or add additional context in comments.

1 Comment

That's wrong! The i variable will contain "points.length - 1" value for all onClick() function.
1
function add_marker() {
    var points = [
        ["P1", 51.57186, 11.8517, '<img src="img/1.JPG" alt="" width="350px">'],
        ["P2", 51.57886, 11.8117, '<img src="img/2.JPG" alt="" width="350px">'],
        ["P3", 51.57586, 11.8017, '<img src="img/3.JPG" alt="" width="350px">']
    ];
    var marker = [];
    var i;
    for (i = 0; i < points.length; i++) {
        marker[i] = new L.Marker([points[i][1], points[i][2]])
        marker[i].addTo(map);
        marker[i].bindPopup(points[i][0]);
        // using a closure to avoid incorrect reference
        (function (i) {
            marker[i].on('click', function(e){
                onClick(e, points, i);
            });
        })(i)
    };
};

function onClick(e, points, i) {
    $('#plot').html(points[i][3]);
};

https://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/ explaining why using the closure

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.