Skip to content

Handling layer additions

FilipLeitner edited this page May 7, 2024 · 1 revision

Hslayers library includes a set of map event handlers that are registered for each layer loaded into the OL map.

These handlers are responsible for:

  • on('add'): Proper initialization of the layer, including setting zIndex, integrating into the layer manager structure, or triggering the this.hsEventBusService.layerAdditions event.
  • `on('remove'): Proper removal of the layer.
  • change:resolution: Debounced resolution based changes

Adding a layer to the Hslayers application is primarily done through the Hslayers configuration object properties, as detailed below:

  ...
  default_layers?: Layer<Source>[];
  box_layers?: Group[];
  base_layers?: {
    url: string;
    default: string;
  };
  ...

These layers will be registerd automatically and there is no need for any adjustments.

However, if you are adding a layer to the map outside of this configuration, you need to ensure that the Hslayers event registration has been initialized. This can be accomplished by responding to the emission of hsEventBusService.mapEventHandlersSet subject.

   this.hsEventBusService.mapEventHandlersSet
     .pipe(take(1))
     .subscribe((_) => {
       //Create layer here
       this.createLayer();
     });

where createLayer method can be anything such as

  /**
   * Create layer for displaying sensor data
   */
  createLayer() {
    this.layer = new VectorLayer<VectorSource>({
      properties: {
        path: 'Sensors',
        title: 'Sensor units',
        popUp: {
          attributes: ['*'],
        },
        editor: {
          editable: false,
        },
        sld: sensorUnitStyle,
      },
      source: new VectorSource({}),
    });
    this.hsMapService.getMap().addLayer(this.layer);
  }
Clone this wiki locally