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

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

CSS

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

.mi-control-personalizado {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: #4CAF50;
  color: white;
  border: none;
  padding: 10px;
  font-size: 16px;
  border-radius: 4px;
  cursor: pointer;
}

.mi-control-personalizado button {
  background: none;
  border: none;
  color: inherit;
  font-size: inherit;
  cursor: pointer;
}


              
            
!

JS

              
                class MiControlPersonalizado extends ol.control.Control {
  constructor(opt_options) {
    const options = opt_options || {};

    // Crear el botón
    const button = document.createElement('button');
    button.innerHTML = 'C'; // Texto o ícono del botón
    button.title = 'Centrar el mapa'; // Tooltip al pasar el ratón

    // Crear el contenedor del control
    const element = document.createElement('div');
    element.className = 'mi-control-personalizado ol-unselectable ol-control';
    element.appendChild(button);

    // Llamar al constructor de la clase base
    super({
      element: element,
      target: options.target,
    });

    // Manejar el evento de clic en el botón
    button.addEventListener('click', this.handleClick.bind(this), false);
  }

  handleClick() {
    // Acción del botón: centrar el mapa
    map.getView().setCenter([0, 0]);
    map.getView().setZoom(2);
  }
}

// Crear el mapa y agregar el control personalizado
const map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM(),
    }),
  ],
  target: 'map',
  view: new ol.View({
    center: [0, 0],
    zoom: 2,
  }),
  controls: [
    new MiControlPersonalizado(), // Agregar el control personalizado
  ],
});

              
            
!
999px

Console