0

I would like to know how to solve the clash between Leaflet control plugin and map.on zoomend function

The problem is related to this query:

Displaying selected layers depending on zoom in Leaflet

and as it has been solved, there is still one issue to fix.

I discovered, that once the leaflet search plugin is attached to the map:

  L.control.search({
    layer: L.layerGroup([job2]),
    initial: false,
    propertyName: 'myKey',
    zoom: 18,
    position: 'topleft'
    }).addTo(map);

the map.on('zoomend', function() { doesn't work at the very initial stage (when open or refresh the map to default link).

The problem is shown in the image below:

enter image description here

Under the zoom level 7 all dots are still visible, whereas I set zoomend for zoom 9. When I start zooming in/out on the map, it starts work correctly as perr below:

enter image description here

My code looks as follow:

   L.control.search({
    layer: L.layerGroup([job2]),
    initial: false,
    propertyName: 'myKey',
    zoom: 18,
    position: 'topleft'
    }).addTo(map);

   map.on('zoomend', function() {
   if (map.getZoom() < 9){
if (map.hasLayer(job2)) map.removeLayer(job2);
   }
    else {
     if (document.querySelector("input[name=vm]").checked && 
 !map.hasLayer(job2)) map.addLayer(job2);
   }
  });

When enter the leaflet-search.js plugin, I see the section, which I believe is responsible for this affliction:

   showLocation: function(latlng, title) {  //set location on map from _recordsCache
    var self = this;
    /*
    self._map.once('moveend zoomend', function(e) {

        if(self._markerSearch) {
            self._markerSearch.addTo(self._map).setLatLng(latlng);
        }

    });
    */
    self._moveToLocation(latlng, title, self._map);
    //FIXME autoCollapse option hide self._markerSearch before visualized!!
    if(self.options.autoCollapse)
        self.collapse();

          return self;
      }

Regarding to this I switched off part of the code with: self._map.once('moveend zoomend', function(e) { but the problem remains the same.

Could somebody advice how to remove this issue and make the layers hidden at the initial stage (after refreshing the map)?

Link to my map is here:

https://mariusz-krukar.mkrgeo.pl/en/workshop/media/MDU_demo.html#55.21022,-1.57385,6z

Once open, you will see the dots still visible, whereas zoom level is 6. When yoyu turn into zoom 7 and back to zoom 6 they will be gone until refresh the map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.