Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section id="layerswitcher">
  <h1>Base Layers Group</h1>
  <label>Group Visible</label>
  <input id="Group" type="checkbox" />
  
  <h2>Layers</h2>
  <section>
    <label>OSM</label>
    <input id="OSM" type="checkbox"/>
  </section>
  
  <section>
    <label>IGNBaseTodo</label>
    <input id="IGNBaseTodo" type="checkbox" checked/>
   </section>
</section>

<div id="map"></div>

              
            
!

CSS

              
                html, body, #map { width:100%; height:100%; margin:0; }

#layerswitcher {
  width: 240px;
  height: 200px; 
  position: absolute;
  z-index: 10;
  
  background-color: white;
  padding: 5px;
}


              
            
!

JS

              
                  // Create OSM layer
const osm = new ol.layer.Tile({
  source: new ol.source.OSM(),
  visible: false,
 });

// Create IGN Base Todo layer
const baseIGNTodo = new ol.layer.Tile({
    source: new ol.source.XYZ({
    url: 'https://tms-ign-base.ign.es/1.0.0/IGNBaseTodo/{z}/{x}/{-y}.jpeg'
  })
});

// Group layers
const BaseLayerGroup = new ol.layer.Group({});

const collection = new ol.Collection();

// Establecer más capas en el grupo utilizando setLayers
BaseLayerGroup.setLayers(collection);

// Se pueden ir añadiendo y eliminado elementos
// desde un collection
collection.push(osm)
collection.push(baseIGNTodo)

// Create map
const map = new ol.Map({
  layers: [BaseLayerGroup],
  target: 'map',
    view: new ol.View({
    center: ol.proj.fromLonLat([0, 0]),
    zoom: 2
  })
});

// Helper function to get element by selector
const $ = (id) => document.querySelector(id);

// Event listeners for checkboxes
const osmInput = $('#OSM');
osmInput.addEventListener('click', () => osm.setVisible(osmInput.checked));

const baseIGNTodoInput = $('#IGNBaseTodo');
baseIGNTodoInput.addEventListener('click', () => baseIGNTodo.setVisible(baseIGNTodoInput.checked));

const GroupInput = $('#Group');
GroupInput.addEventListener('click', () => {
  const checked = GroupInput.checked;
  BaseLayerGroup.setVisible(GroupInput.checked);
  
  osmInput.checked = checked;
  baseIGNTodoInput.checked = checked;
});
              
            
!
999px

Console