Edit

Mapbox Vector Layer

mapbox7 studio1 vector80 tiles7

Rendering a layer with a Mapbox-hosted style.

The MapboxVector layer from the ol-mapbox-style package allows you to create a layer based on a Mapbox-hosted style using a single vector source. If your style uses more than one source, use the source property to choose a single vector source. Use the layers property if you only want to render a subset of the style's layers (provided they all share the same source).

main.js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {MapboxVectorLayer} from 'ol-mapbox-style';

const map = new Map({
  target: 'map',
  layers: [
    new MapboxVectorLayer({
      styleUrl: 'mapbox://styles/mapbox/bright-v9',
      accessToken:
        'Your Mapbox access token from https://mapbox.com/ here',
    }),
  ],
  view: new View({
    center: [0, 0],
    zoom: 2,
  }),
});
index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Mapbox Vector Layer</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
{
  "name": "mapbox-vector-layer",
  "dependencies": {
    "ol": "10.3.1",
    "ol-mapbox-style": "^12.3.1"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}