How to use the OGC WMTS service in your map with OpenLayers

This tutorial shows how to add an OGC WMTS from MapTiler to your web map using OpenLayers. The easiest way to add a WMTS to a map is to get the configuration information directly from the GetCapabilities response.

  1. Copy the following code, paste it into your favorite text editor, and save it as a .html file.

Check out the step-by-step tutorial How to use OpenLayers

  1. Install the npm package.

  2. Include the CSS file.

    If you have a bundler that can handle CSS, you can import the CSS or include it with a <link> in the head of the document via the CDN

  3. Include the following code in your JavaScript file (Example: main.js).

  1. Replace YOUR_MAPTILER_API_KEY_HERE with your actual MapTiler API key.

  2. The next is up to you. You can center your map wherever you desire (modifying the starting position) and set an appropriate zoom level (modifying the starting zoom) to match your users’ needs. Additionally, you can change the map’s look (by updating the source URL); choose from a range of visually appealing map styles from our extensive MapTiler standard maps, or create your own to truly differentiate your application.

  1. Create the WMTS capabilities parser.

  2. Get the WMTS Capabilities information and read it with the parser.

  3. Change the source to use the WMTS

  1. Import the new modules needed to add the new functionality.

    
     import Map from 'ol/Map.js';
     import View from 'ol/View.js';
     import TileLayer from 'ol/layer/Tile.js';
     import WMTS, {optionsFromCapabilities} from 'ol/source/WMTS.js';
     import Attribution from 'ol/control/Attribution.js';
     import WMTSCapabilities from 'ol/format/WMTSCapabilities.js';
     import { defaults as defaultControls } from 'ol/control/defaults.js';
     import { fromLonLat } from 'ol/proj.js';
     
  2. Create the WMTS capabilities parser.

  3. Get the WMTS Capabilities information and read it with the parser.

  4. Change the source to use the WMTS

The WMTS functionality requires at least a FLEX plan subscription. For more information, read about the different MapTiler plans.

Learn more

If you want to learn more about OGC WMTS check out the WMTS article.