bjornd / jvectormap

jvectormap.com
GNU Affero General Public License v3.0
1.38k stars 431 forks source link

How can I clear selected regions/markers on region/marker select? #478

Closed johnbburg closed 1 year ago

johnbburg commented 1 year ago

I am trying to make regions and markers behave similarly with the option to have only one region/marker selected. So when a user selects a region, any previously selected regions or markers become unselected, and vice versa.

I thought I might be able to achieve this by calling map.clearSelectedMarkers(); and map.clearSelectedRegions(); from inside onRegionSelected() and onMarkerSelected() respectively. However, it seems that calling this function triggers a chain of function calls that eventually calls the opposing on*Selected() function, which in turn clears the selected marker or region itself.

Is there a way to avoid this? So I can clear all selected markers and regions without triggering this?

Thinking out loud here, maybe I just need to check if the selected regions/markers is empty before I attempt to clear it. I might have just answered my own question, but I typed this out.

Yes indeed, that's all I needed:

              if (selectedRegions.length !== 0) {
                map.clearSelectedMarkers();
              }