stefanocudini / leaflet-search

Search stuff in a Leaflet map
https://opengeo.tech/maps/leaflet-search/
MIT License
780 stars 335 forks source link

Circle drawn on the hit marker does not disappear when cancelling #293

Open metametis opened 2 years ago

metametis commented 2 years ago

Hi, I use the marker.circle option. When I click on the cancel icon in the search box, the circle stays on the map, even with the option 'hideMarkerOnCollapse: true'. However, when I make another research, the circle correctly disappears. Is there a way to fix this ? THank you, Olivier

hupe13 commented 1 year ago

Maybe is it to late: hideMarkerOnCollapse does the job. But there is an other problem with it. If I zoom the map, the circle appears again. To prevent this change https://github.com/stefanocudini/leaflet-search/blob/e5573af77780fbe5796f6ad5bf3175f6745b3ae4/src/leaflet-search.js#L903 to

if(self._markerSearch && title != "") {
metametis commented 1 year ago

Thank you. I fixed the problem already but to tell the truth, I don't remember how... Olivier

hupe13 commented 1 year ago

I tested it again with my application, not that it is a special case. ... I changed my code, now sometimes it works and sometimes it doesn't. Sorry for trouble. Edit: Solved, now it works.

senavarreteh commented 1 year ago

Hello. I have this same issue when i zoom, the circle just appears again. Couldn't fix the problem with what was suggested. Is there any other way to fix this?

Im currently using leaflet 1.9.3 and leaflet-search 3.0.9

Thanks for the help. Sergio.

hupe13 commented 1 year ago

I don't remember what I did specifically that the circle is now gone. Maybe this:

          markerSearchControl.on("search:cancel", function(e) {
            if (e.target.options.hideMarkerOnCollapse) {
              //console.log(e.target._map);
              e.target._map.removeLayer(this._markerSearch);
            }
          });
senavarreteh commented 1 year ago

Hello, thanks for answering!

Unfortunately those lines of code did not solve my problem.

I think I have found the source.

https://github.com/stefanocudini/leaflet-search/blob/e5573af77780fbe5796f6ad5bf3175f6745b3ae4/src/leaflet-search.js#L901

The "once" listener has 2 types: moveend and zoomend.

In previous versions of leaflet, if one or the other is executed, the listener is removed when the function finishes executing.

For the current version, both events must be executed to remove the listener. This causes the markers to be added again when zooming.

I don't know if this is intentional or a bug. For now I have solved the issue by removing the zoomend event from the listener.

You can try with this example and see two logs in your console (latest leaflet version).

map.once('moveend zoomend', function (e) { console.log(e.type); });

I hope this information is useful. And excuse my spelling mistakes.

Greetings.