Atlaslog
Atlaslog / Examples / OpenLayers / Pmtiles Image
OpenLayers · Runnable Example

Pmtiles Image

Runnable OpenLayers example: Pmtiles Image. 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-pmtiles-image.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-pmtiles-image.html
<!--
  示例:PMTiles 影像瓦片
  要点:TileImage + tileUrlFunction 返回按瓦片坐标即时绘制的图片
  依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 Key)
  替代:官方读 PMTiles 归档取瓦片,这里用 canvas 生成同坐标的瓦片图片
-->
<!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>PMTiles 影像瓦片</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;color:#16181d}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
  .bar b{color:#16181d;font-weight:600}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label><input type="checkbox" id="tgl" checked> 瓦片图层</label>
  <span>已载瓦片 <b id="cnt">0</b></span>
  <span>透明度 <input type="range" id="op" min="0" max="100" value="80" style="width:90px"></span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';
  if (!window.ol) {
    document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
    return;
  }

  var cache = {};
  var loaded = 0;

  /* 按 z/x/y 画一张 256×256 的瓦片,返回 data URL —— PMTiles 归档替换成的即时绘制 */
  function tileUrl(z, x, y) {
    var key = z + '/' + x + '/' + y;
    if (cache[key]) return cache[key];

    var c = document.createElement('canvas');
    c.width = 256;
    c.height = 256;
    var ctx = c.getContext('2d');
    var hue = (x * 37 + y * 71 + z * 13) % 360;

    ctx.fillStyle = 'hsl(' + hue + ',42%,72%)';
    ctx.fillRect(0, 0, 256, 256);
    ctx.strokeStyle = 'rgba(255,255,255,.85)';
    ctx.lineWidth = 4;
    ctx.strokeRect(2, 2, 252, 252);
    ctx.strokeStyle = 'rgba(20,26,36,.18)';
    ctx.lineWidth = 1;
    for (var i = 1; i < 4; i++) {
      ctx.beginPath();
      ctx.moveTo(i * 64, 0);
      ctx.lineTo(i * 64, 256);
      ctx.moveTo(0, i * 64);
      ctx.lineTo(256, i * 64);
      ctx.stroke();
    }
    ctx.fillStyle = 'rgba(20,26,36,.78)';
    ctx.font = '600 26px sans-serif';
    ctx.fillText(z + '/' + x + '/' + y, 16, 132);

    var url = c.toDataURL();
    cache[key] = url;
    return url;
  }

  var source = new ol.source.TileImage({
    projection: 'EPSG:3857',
    tileGrid: ol.tilegrid.createXYZ({ tileSize: 256, maxZoom: 14 }),
    tileUrlFunction: function (tc) {
      if (!tc) return undefined;
      return tileUrl(tc[0], tc[1], tc[2]);
    }
  });

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

  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'}) }), layer],
    view: new ol.View({ center: ol.proj.fromLonLat([116.39, 39.91]), zoom: 4 }),
    controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
  });

  source.on('tileloadend', function () {
    loaded++;
    document.getElementById('cnt').textContent = loaded;
  });

  document.getElementById('tgl').addEventListener('change', function () {
    layer.setVisible(this.checked);
  });

  document.getElementById('op').addEventListener('input', function () {
    layer.setOpacity(this.value / 100);
  });
})();
</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 · EXAMPLEPolygon Pattern OpenLayers · EXAMPLEPolygon Styles OpenLayers · EXAMPLEPopulated Places