0

I have a number of markers on a leaflet map with a sidebar that is supposed to update with whatever markers are within the current map bounds.

This is adapted from Bryan McBride's Bootleaf template https://github.com/bmcbride/bootleaf

syncSidebar() is the function that is supposed to loop through the current layer and add to the sidebar all the applicable points.

However, my code only returns a small subset of the markers that should be included, namely the ones in the far northwest corner. It seems like contains(coord) is only returning true for a fraction of the markers that should exist within map.getBounds().

Thank you so much for any insight.

function syncSidebar() {
    /* Empty sidebar features */
    $("#feature-list tbody").empty();
    /* Loop through kazakhstan_polygon layer and add only features which are in the map bounds */

    kazakhstan_points_only.eachLayer(function(layer) {
        if (map.hasLayer(kazakhstan_polygon)) {
            var coord = layer.feature.geometry.coordinates;
            if (map.getBounds().contains(coord)) {
                $("#feature-list tbody").append('<tr class="feature-row" id="' + L.stamp(layer) + '" lat="' + layer.feature.geometry.coordinates[1] + '" lng="' + layer.feature.geometry.coordinates[0] + '"><td style="vertical-align: middle;"><img width="16" height="18" src="assets/img/eil-school.png"></td><td class="feature-name">' + layer.feature.properties.NAME + '</td><td style="vertical-align: middle;"><i class="fa fa-chevron-right pull-right"></i></td></tr>');
            
            }
        }
    });

    /* Update list.js featureList */
    featureList = new List("features", {
        valueNames: ["feature-name"]
    });
    featureList.sort("feature-name", {
        order: "asc"
    });
}
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works. – Ian Turton Jul 8 at 7:20
  • Without actual data (contents of kazakhstan_points layer) it's impossible to say why behavior is not as expected. One thing is sure: condition if (map.hasLayer(kazakhstan_polygon)) should be outside kazakhstan_points_only.eachLayer loop, since kazakhstan_polygon does not change inside loop. – TomazicM Jul 8 at 12:10

Your Answer

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

Browse other questions tagged or ask your own question.