Map Export

Download PNG

Example of exporting a map as a PNG image.

Example of exporting a map as a PNG image.

<!DOCTYPE html>
<html>
  <head>
    <title>Map Export</title>
    <link rel="stylesheet" href="http://openlayers.org/en/v3.16.0/css/ol.css" type="text/css">
    <script src="http://openlayers.org/en/v3.16.0/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <div id="no-download" class="alert alert-danger" style="display: none">
      This example requires a browser that supports the
      <a href="http://caniuse.com/#feat=download">link download</a> attribute.
    </div>
    <a id="export-png" class="btn btn-default" download="map.png"><i class="fa fa-download"></i> Download PNG</a>
    <script>
      var map = new ol.Map({
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          }),
          new ol.layer.Vector({
            source: new ol.source.Vector({
              url: 'data/geojson/countries.geojson',
              format: new ol.format.GeoJSON()
            })
          })
        ],
        target: 'map',
        controls: ol.control.defaults({
          attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
            collapsible: false
          })
        }),
        view: new ol.View({
          center: [0, 0],
          zoom: 2
        })
      });

      var exportPNGElement = document.getElementById('export-png');

      if ('download' in exportPNGElement) {
        exportPNGElement.addEventListener('click', function() {
          map.once('postcompose', function(event) {
            var canvas = event.context.canvas;
            exportPNGElement.href = canvas.toDataURL('image/png');
          });
          map.renderSync();
        }, false);
      } else {
        var info = document.getElementById('no-download');
        /**
         * display error message
         */
        info.style.display = '';
      }
    </script>
  </body>
</html>