Function applyStyle

  • Applies a style function to an ol/layer/VectorTile or ol/layer/Vector with an ol/source/VectorTile or an ol/source/Vector. If the layer does not have a source yet, it will be created and populated from the information in the glStyle (unless updateSource is set to false).

    Example:

    import {applyStyle} from 'ol-mapbox-style';
    import {VectorTile} from 'ol/layer.js';

    const layer = new VectorTile({declutter: true});
    applyStyle(layer, 'https://api.maptiler.com/maps/basic/style.json?key=YOUR_OPENMAPTILES_TOKEN');

    The style function will render all layers from the glStyle object that use the source of the first layer, the specified source, or a subset of layers from the same source. The source needs to be a "type": "vector" or "type": "geojson" source.

    Two additional properties will be set on the provided layer:

    • mapbox-source: The id of the Mapbox/MapLibre Style document's source that the OpenLayers layer was created from. Usually apply() creates one OpenLayers layer per Mapbox/MapLibre Style source, unless the layer stack has layers from different sources in between.
    • mapbox-layers: The ids of the Mapbox/MapLibre Style document's layers that are included in the OpenLayers layer.

    Parameters

    • layer: VectorLayer<any> | VectorTileLayer<any>

      OpenLayers layer. When the layer has a source configured, it will be modified to use the configuration from the glStyle's source. Options specified on the layer's source will override those from the glStyle's source, except for url and tileUrlFunction. When the source projection is the default (EPSG:3857), the tileGrid will also be overridden. If you'd rather not have ol-mapbox-style modify the source, configure applyStyle() with the updateSource: false option.

    • glStyle: any

      Mapbox/MapLibre Style object.

    • OptionalsourceOrLayersOrOptions: string | string[] | Options & ApplyStyleOptions = ''

      Options or source key or an array of layer ids from the Mapbox/MapLibre Style object. When a source key is provided, all layers for the specified source will be included in the style function. When layer ids are provided, they must be from layers that use the same source. When not provided or a falsey value, all layers using the first source specified in the glStyle will be rendered.

    • OptionaloptionsOrPath: string | Options & ApplyStyleOptions = {}

      Deprecated. Options. Alternatively the path of the style file (only required when a relative path is used for the "sprite" property of the style).

    • Optionalresolutions: number[] = undefined

      Deprecated. Resolutions for mapping resolution to zoom level. Only needed when working with non-standard tile grids or projections, can also be supplied with options.

    Returns Promise<any>

    Promise which will be resolved when the style can be used for rendering.