I have a strange issue with my Leaflet/Javascript code. I'm trying to
refactor my application which was initially getting line data from
PostGIS as a certain layer checkbox has been checked:
Then it bound some popups etc. It was working fine, but each time it
was collecting all checked boxes values and sending it through ajax to
PostGIS query and getting JSON response into queryLayer. So that each time some checkbox has been changed, the current queryLayer has been removed and loaded again with new data.
After my refactor I'm now able to:
- Create a dedicated variable for layer as the checkbox is clicked using window[checkboxValue] (which allows me to create var names dynamically) and populate it with PostGIS geometry using ajax.
- Once a particular checkbox is unchecked - then I'm removing this window[uncheckedValue] by layer.id var.
My problem is that after checking the second layer checkbox it is
added into the map correctly and its popups/click events work fine, but first (previous) layer, although is visible as desired, popups/click events are not possible anymore for it.
NOTE: I've tried both:
- separate layers: window[checkedTrasa]= L.geoJSON(JSON.parse(response), {...}
- L.featureGroup() and adding/removing layers from group
My code is on above's stackoverflow link.