Search code examples
javascriptmapsopenlayers

How to add FULLSCREEN control properly?


I am trying to add a full screen control (I had a switcher too). when i create this control inside map creation new ol.MAP...(following documentation) i dont get errors but they(layersSwitcher and Fullscreen control) dont appear.

map = new ol.Map({
        controls:[new ol.control.LayerSwitcher({
            tipLabel: 'Leyenda',
    
        }),
        new ol.control.defaults().extend([new ol.control.FullScreen()])],
        layers: [
            new ol.layer.Group({
                'title': 'Mapas de base',
                layers: [grisOSM, mapaOSM]
            }),

I also tried creating the map, then create and add controls like this:

 map = new ol.Map({
        layers: [
            new ol.layer.Group({
                'title': 'Mapas de base',
                layers: [grisOSM, mapaOSM]
            }),

    const layerSwitcher = new ol.control.LayerSwitcher({
        tipLabel: 'Leyenda',

    });
    map.addControl(layerSwitcher);
    const fullScreen = new ol.control.defaults().extend([new ol.control.FullScreen()]);

    map.addControl(fullScreen);

I get Uncaught TypeError: t.element.setMap is not a function error and can't see none of them


Solution

  • Either

     map = new ol.Map({
            controls: ol.control.defaults().extend([new ol.control.FullScreen()]),
            layers: [
                new ol.layer.Group({
                    'title': 'Mapas de base',
                    layers: [grisOSM, mapaOSM]
                }),
             ],
         });
    

    or

     map = new ol.Map({
            layers: [
                new ol.layer.Group({
                    'title': 'Mapas de base',
                    layers: [grisOSM, mapaOSM]
                })
            ],
        });
    
        map.addControl(new ol.control.FullScreen());
    

    ol.control.defaults is not a constructor so does not need new