Function apply

  • Loads and applies a Mapbox/MapLibre Style object into an OpenLayers Map or LayerGroup. This includes the map background, the layers, and for Map instances that did not have a View defined yet also the center and the zoom.

    Example:

    import apply from 'ol-mapbox-style';

    apply('map', 'mapbox://styles/mapbox/bright-v9', {accessToken: 'YOUR_MAPBOX_TOKEN'});

    The center and zoom will only be set if present in the Mapbox/MapLibre Style document, and if not already set on the OpenLayers map.

    Layers will be added to the OpenLayers map, without affecting any layers that might already be set on the map.

    Layers added by apply() will have two additional properties:

    • 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.

    This function sets an additional mapbox-style property on the OpenLayers Map or LayerGroup instance, which holds the Mapbox/MapLibre Style object.

    Parameters

    • mapOrGroupOrElement: string | Map | LayerGroup | HTMLElement

      Either an existing OpenLayers Map instance, or a HTML element, or the id of a HTML element that will be the target of a new OpenLayers Map, or a layer group. If layer group, styles releated to the map and view will be ignored.

    • style: any

      JSON style object or style url pointing to a Mapbox/MapLibre Style object. When using Mapbox APIs, the url is the styleUrl shown in Mapbox Studio's "share" panel. In addition, the accessToken option (see below) must be set. When passed as JSON style object, all OpenLayers layers created by apply() will be immediately available, but they may not have a source yet (i.e. when they are defined by a TileJSON url in the Mapbox/MapLibre Style document). When passed as style url, layers will be added to the map when the Mapbox/MapLibre Style document is loaded and parsed.

    • options: Options = {}

      Options.

    Returns Promise<Map | LayerGroup>

    A promise that resolves after all layers have been added to the OpenLayers Map instance or LayerGroup, their sources set, and their styles applied. The resolve callback will be called with the OpenLayers Map instance or LayerGroup as argument.