0

I created a toggle button to add and remove wms layer from the map. In the default zoom level, it is working fine. But when I remove layer and zoom in or out, then do "Add Layer", the layer does not show up until map is zoomed in or out. I tried layer.refresh(), but no improvement. Below is my sample code.

var map = L.mapbox.map('map', 'examples.map-4l7djmvo').setView([24, -89], 5);
var mylayer =      L.tileLayer.wms("http://nowcoast.noaa.gov/wms/com.esri.wms.Esrimap/analyses", {
format: 'image/png',
transparent: true,
layers: 'NSSL_RAS_QPE_72HR'    
});

$('#Check').click(function() {    
   if($(this).text() == "Add Layer")            {
         $(this).text("Remove Layer"); 
         map.addLayer(mylayer); 
         mylayer.refresh();
       }
       else
           {
             $(this).text("Add Layer"); 
             map.removeLayer(mylayer);
              mylayer.refresh();
           }        
});
1

Just making a cursory glance you might try changing your function to this instead:

$(document).ready(function() {

   $('#Check').click(function() {    
      if($(this).text() == "Add Layer")            {
            $(this).text("Remove Layer"); 
            map.addLayer(mylayer); 
            mylayer.refresh();
          }
          else
              {
                $(this).text("Add Layer"); 
                map.removeLayer(mylayer);
                mylayer.refresh();
              }        
   });
});

Without the document ready part, you script is executed before the page is ever rendered. This can cause weird issues. From now on place any extra controls you make inside the document ready block.

Also, have your tried playing with Leaflet's layer control that is built in. Luckily, they have a nice tutorial for that. Makes it tidy and neat. But at the same time I am also not aware of your end goal.

  • Thanks for the suggestion for document ready part. Even placing my script under document ready it still is not showing my WMS layer until I zoom in. Point Overlay is showing. Problem is only with WMS. I need grouping of different types of layers and some additional features which leaflet control does not have. So, I am forced to create my own layer control. – Prabhash Jha Jun 3 '13 at 19:23
0

I had such a problem when my wms layer didnt have pre rendered tiles on that specific zoom (I used geoserver , It was solved when I cached all zoom levels) Maybe as a workaround try setting zoom using map.setZoom() after adding/removing the layer.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.