Saturday 7 January 2017

javascript - Leaflet Sidebar - set content depending on clicked GeoJSON feature


My goal is to set the content of the sidebar depending on what GeoJSON feature was clicked on.


For example when clicked on polygon A the content of the sidebar is: "This is A" and when clicked on polygon B the content of the sidebar is: "This is B"


my approach to the problem (SOLVED):




  1. function for click event to select GeoJSON feature:


        function onEachFeature(feature, layer) {
    layer.on({

    mouseover: highlightFeature,
    mouseout: resetHighlight,
    click: openSidebar
    });
    }


  2. function to open sidebar:


        function openSidebar(e) {
    sidebar.toggle();

    if (e.target.feature.properties.name == "House 24") {
    sidebar.setContent('this is House 24');
    }
    }


  3. loaded GeoJSON:


        var my_geojson = L.geoJson(my_geojson,{
    style: myStyle,
    onEachFeature: onEachFeature

    }).addTo(map);



Answer



Might just need e.target or e.layer before feature.properties.name - console.log is your friend here and will likely answer your question.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...