Single Image WMS with Proj4js

Example of integrating Proj4js for coordinate transforms.

With Proj4js integration, OpenLayers can transform coordinates between arbitrary projections.

<!DOCTYPE html>
<html>
  <head>
    <title>Single Image WMS with Proj4js</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css">
    <!-- 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://openlayers.org/en/v4.6.4/build/ol.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.4.4/proj4.js"></script>
    <script src="https://epsg.io/21781-1753.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script>
      // Transparent Proj4js support:
      //
      // EPSG:21781 is known to Proj4js because its definition was loaded in the html.
      // Now when we create an ol.proj.Projection instance with the 'EPSG:21781' code,
      // OpenLayers will pick up parameters like units and transform functions from
      // Proj4js.
      //
      // Note that we are setting the projection's extent here, which is used to
      // determine the view resolution for zoom level 0. Recommended values for a
      // projection's validity extent can be found at https://epsg.io/.
      //
      // If you use Proj4js only to transform coordinates, you don't even need to
      // create an ol.proj.Projection instance. ol.proj.get() will take care of it
      // internally.

      var projection = new ol.proj.Projection({
        code: 'EPSG:21781',
        extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864]
      });

      var extent = [420000, 30000, 900000, 350000];
      var layers = [
        new ol.layer.Image({
          extent: extent,
          source: new ol.source.ImageWMS({
            url: 'https://wms.geo.admin.ch/',
            crossOrigin: 'anonymous',
            attributions: '© <a href="http://www.geo.admin.ch/internet/geoportal/' +
                'en/home.html">Pixelmap 1:1000000 / geo.admin.ch</a>',
            params: {
              'LAYERS': 'ch.swisstopo.pixelkarte-farbe-pk1000.noscale',
              'FORMAT': 'image/jpeg'
            },
            serverType: 'mapserver'
          })
        }),
        new ol.layer.Image({
          extent: extent,
          source: new ol.source.ImageWMS({
            url: 'https://wms.geo.admin.ch/',
            crossOrigin: 'anonymous',
            attributions: '© <a href="http://www.geo.admin.ch/internet/geoportal/' +
                'en/home.html">National parks / geo.admin.ch</a>',
            params: {'LAYERS': 'ch.bafu.schutzgebiete-paerke_nationaler_bedeutung'},
            serverType: 'mapserver'
          })
        })
      ];

      var map = new ol.Map({
        controls: ol.control.defaults().extend([
          new ol.control.ScaleLine()
        ]),
        layers: layers,
        target: 'map',
        view: new ol.View({
          projection: projection,
          center: ol.proj.fromLonLat([8.23, 46.86], projection),
          extent: extent,
          zoom: 2
        })
      });
    </script>
  </body>
</html>