Ajax query dynamic layers - popup displays only for last loaded layer

55 views
Skip to first unread message

Jakub Olech

unread,
Oct 2, 2020, 11:34:22 AM10/2/20
to Leaflet

Hello All,


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:

OdLAx.png

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:

  1. 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.
  2. 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.

Thanks in advance!
Reply all
Reply to author
Forward
0 new messages