2

I have a leaflet map that zooms and fits on the bounds of the polygon that is clicked via onEachFeature. Is there a way to add another geojson layer by clicking on the polygon? I need to zoom in and add another layer on click

    var polska;

    function resetHighlight(e) {
        polska.resetStyle(e.target);
        info.update();
    }

    function zoomToFeature(e) {
        map.fitBounds(e.target.getBounds());
    }

    function onEachFeature(feature, layer) {
        layer.on({
            mouseover: highlightFeature,
            mouseout: resetHighlight,
            click: zoomToFeature
        });
    }

    polska = L.geoJson(exp_polska, {
        style: style,
        onEachFeature: onEachFeature
    }).addTo(map);
3

Just run that code inside of zoomToFeature, the function that is called on click.

    function zoomToFeature(e) {
      map.fitBounds(e.target.getBounds());
      L.geoJson(polska2, {
        style: myStyle2,
        onEachFeature: onEachFeature
     }).addTo(map);
    }

See a fiddle here, load a 2nd layer on click of 1st layer

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.