Atlaslog
Atlaslog / Examples / OpenLayers / WMTS Ign
OpenLayers · Runnable Example

WMTS Ign

Runnable OpenLayers example: WMTS Ign. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-wmts-ign.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-wmts-ign.html
<!--
  示例:WMTS 瓦片网格 — OpenLayers wmts-ign
  核心逻辑:自定义 WMTS 瓦片网格(原点 + 分辨率 + 矩阵标识),瓦片内容由页面内 Canvas 生成
  依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WMTS 瓦片网格</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
  html,body{height:100%;margin:0}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label><input type="checkbox" id="osm" checked> 叠加 OSM 底图</label>
  <span id="info">—</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.js"></script>
<script>
(function () {
  'use strict';

  if (typeof ol === 'undefined') {
    document.getElementById('map').innerHTML =
      '<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  var resolutions = [];
  for (var z = 0; z <= 18; z++) {
    resolutions.push(156543.03392804097 / Math.pow(2, z));
  }

  /* WMTS 网格:原点在左上角,矩阵标识自带层级前缀 */
  var grid = new ol.tilegrid.WMTS({
    origin: ol.extent.getTopLeft(new ol.proj.get('EPSG:3857').getExtent()),
    resolutions: resolutions,
    matrixIds: resolutions.map(function (_, i) { return 'EPSG:3857:' + i; }),
    tileSize: 256
  });

  function tilePng(z, x, y) {
    var c = document.createElement('canvas');
    c.width = 256;
    c.height = 256;
    var ctx = c.getContext('2d');

    ctx.fillStyle = '#eef1ea';
    ctx.fillRect(0, 0, 256, 256);
    ctx.strokeStyle = '#a9b4a1';
    ctx.lineWidth = 2;
    ctx.strokeRect(1, 1, 254, 254);
    ctx.fillStyle = 'rgba(47,158,68,0.25)';
    ctx.fillRect(24, 24, 208, 208);

    ctx.fillStyle = '#14622b';
    ctx.font = '600 22px sans-serif';
    ctx.fillText('EPSG:3857:' + z, 18, 44);
    ctx.fillStyle = '#58606e';
    ctx.font = '15px sans-serif';
    ctx.fillText('TILECOL ' + x, 18, 70);
    ctx.fillText('TILEROW ' + y, 18, 92);

    return c.toDataURL();
  }

  var wmtsSource = new ol.source.TileImage({
    projection: 'EPSG:3857',
    tileGrid: grid,
    tileUrlFunction: function (coord) {
      return tilePng(coord[0], coord[1], coord[2]);
    }
  });

  var wmtsLayer = new ol.layer.Tile({ source: wmtsSource, opacity: 0.9 });

  var map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }),
      wmtsLayer
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([116.391, 39.907]),
      zoom: 12,
      minZoom: 2,
      maxZoom: 17
    }),
    controls: ol.control.defaults.defaults({ attribution: false })
  });

  function updateInfo() {
    var z = Math.round(map.getView().getZoom());
    document.getElementById('info').textContent =
      '当前矩阵 EPSG:3857:' + z + ' · 分辨率 ' +
      resolutions[z].toFixed(2) + ' m/px';
  }

  map.on('moveend', updateInfo);
  updateInfo();

  document.getElementById('osm').addEventListener('change', function () {
    map.getLayers().item(0).setVisible(this.checked);
  });
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More OpenLayers examples

OpenLayers · EXAMPLEWMTS Layer From Capabilities OpenLayers · EXAMPLEXYZ OpenLayers · EXAMPLEXYZ Esri