Edit

Mapbox-gl Layer

Example of a Mapbox-gl-js layer integration.

Show how to add a mapbox-gl-js layer in an openlayers map. Note: Make sure to get your own API key at https://www.maptiler.com/cloud/ when using this example. No map will be visible when the API key has expired.

index.html<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Mapbox-gl Layer</title>
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://unpkg.com/mapbox-gl@0.54.0/dist/mapbox-gl.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/mapbox-gl@0.54.0/dist/mapbox-gl.css">
    <style>
      .map {
        width: 100%;
        height:400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script src="index.js"></script>
  </body>
</html>
index.jsimport 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import Layer from 'ol/layer/Layer';
import {toLonLat, fromLonLat} from 'ol/proj';
import {Stroke, Style} from 'ol/style';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import GeoJSON from 'ol/format/GeoJSON';

var center = [-98.8, 37.9];
var key = 'Get your own API key at https://www.maptiler.com/cloud/';

var mbMap = new mapboxgl.Map({
  style: 'https://api.maptiler.com/maps/bright/style.json?key=' + key,
  attributionControl: false,
  boxZoom: false,
  center: center,
  container: 'map',
  doubleClickZoom: false,
  dragPan: false,
  dragRotate: false,
  interactive: false,
  keyboard: false,
  pitchWithRotate: false,
  scrollZoom: false,
  touchZoomRotate: false
});

var mbLayer = new Layer({
  render: function(frameState) {
    var canvas = mbMap.getCanvas();
    var viewState = frameState.viewState;

    var visible = mbLayer.getVisible();
    canvas.style.display = visible ? 'block' : 'none';

    var opacity = mbLayer.getOpacity();
    canvas.style.opacity = opacity;

    // adjust view parameters in mapbox
    var rotation = viewState.rotation;
    if (rotation) {
      mbMap.rotateTo(-rotation * 180 / Math.PI, {
        animate: false
      });
    }
    mbMap.jumpTo({
      center: toLonLat(viewState.center),
      zoom: viewState.zoom - 1,
      animate: false
    });

    // cancel the scheduled update & trigger synchronous redraw
    // see https://github.com/mapbox/mapbox-gl-js/issues/7893#issue-408992184
    // NOTE: THIS MIGHT BREAK WHEN UPDATING MAPBOX
    if (mbMap._frame) {
      mbMap._frame.cancel();
      mbMap._frame = null;
    }
    mbMap._render();

    return canvas;
  }
});

var style = new Style({
  stroke: new Stroke({
    color: '#319FD3',
    width: 2
  })
});

var vectorLayer = new VectorLayer({
  source: new VectorSource({
    url: 'data/geojson/countries.geojson',
    format: new GeoJSON()
  }),
  style: style
});

var map = new Map({
  target: 'map',
  view: new View({
    center: fromLonLat(center),
    zoom: 4
  }),
  layers: [mbLayer, vectorLayer]
});
package.json{
  "name": "mapbox-layer",
  "dependencies": {
    "ol": "6.1.1"
  },
  "devDependencies": {
    "parcel": "1.11.0"
  },
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --experimental-scope-hoisting --public-url . index.html"
  }
}