exportpngopenlayersmapbox-gl-js

How to export png from openlayers map with mapbox-gl


I'm trying to switch from raster map to vector map in OpenLayers using mapbox-gl library, but for some reason map export to png is not working properly: map rendered with mapbox-gl is not visible on exported image. I see only other (geojson) layers, but not map. With raster maps it works properly. I'm using export as in example here: https://openlayers.org/en/latest/examples/export-map.html


Solution

  • If you are using a Mapbox-gl layer in OpenLayers as in https://openlayers.org/en/latest/examples/mapbox-layer.html you will need to add

    preserveDrawingBuffer: true,
    

    to the mapboxgl.Map options to make it exportable.

    Then to include it in the OpenLayers export you will need to add canvas.mapboxgl-canvas to the querySelectorAll list.