4

I have map with 2 base layers (osm, bing) and few overlays. I put layerswitcher outside of map in separate div. I wrote script for toggle layers in that div and it works fine. But there is one problem with base layers. If I select bing as a base layer in my div it displays fine but I can't zoom in or out. Point features (ones from the overlay) are moving but base layer is static. Here is my code:

<div class="left-sidebar">
            <br></br>
            <p>BASE LAYERS</p>
            <label class="radio">
              <input type="radio" name="baselayer" value="osm" id="IDosm_layer" onclick="toggleControl(this);" autocomplete="off" checked>
              OpenStreetMap
            </label>

            <label class="radio">
              <input type="radio" name="baselayer" value="bing" id="IDbing_layer" onclick="toggleControl(this);" autocomplete="off">
              Bing Aerial
            </label>
            <br></br>
            <p>OVERLAYS</p>
            <label class="checkbox">
              <input type="checkbox" value="2" id="IDcurrent_position" onclick="toggleControl(this);" autocomplete="off">
              Overlay1
            </label>
</div>  

That's the html part of code for layer tree div and here is javascript code:

function toggleControl(element){            

     if(element.value == "osm"){    
      bing.setVisibility(false);
      osm.setVisibility(true);
     }
     else if(element.value == "bing"){
      osm.setVisibility(false);
      bing.setVisibility(true);         
     }

     for(i=2; i<12; i++) {
        if(element.value == i) {
            map.layers[i].setVisibility(element.checked); 
        }
    }

}

I hope you understood my problem and I'm looking forward to your answer.

2
+50

Try this

function toggleControl(element){            

 if(element.value == "osm"){    
  map.setBaseLayer(new OpenLayers.Layer.OSM());
 }
 else if(element.value == "bing"){
  map.setBaseLayer(new OpenLayers.Layer.Bing());      
 }

}
  • I can't believe how I forgot on setBaseLayer function. setVisibility works fine with overlays but it doesn't really help with base layers. Thank you very much! – user14026 Aug 2 '13 at 22:04

Your Answer

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