Atlaslog
Atlaslog / Examples / OpenLayers / Ogc Map Tiles Geographic
OpenLayers · Runnable Example

Ogc Map Tiles Geographic

Runnable OpenLayers example: Ogc Map Tiles Geographic. 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-ogc-map-tiles-geographic.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-ogc-map-tiles-geographic.html
<!--
  示例:度制瓦片网格
  要点:ol.tilegrid.TileGrid 用度为单位的分辨率与原点,tileCoord 按经纬度求算
  依赖: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>度制瓦片网格</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 input[type=range]{width:110px}
  .bar b{color:#16181d;font-weight:600}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <label for="lvl">网格层级 <b id="lv">4</b></label>
  <input type="range" id="lvl" min="2" max="7" step="1" value="4">
  <span>分辨率 <b id="res">—</b> °/px</span>
  <span>每块覆盖 <b id="span">—</b>°</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 fromLonLat = ol.proj.fromLonLat;

  /* 4326 网格:原点在左上角(-180, 90),分辨率以度/像素计 */
  var resolutions = [];
  for (var z = 0; z <= 10; z++) resolutions.push(180 / (256 * Math.pow(2, z)));

  var grid = new ol.tilegrid.TileGrid({
    extent: [-180, -90, 180, 90],
    origin: [-180, 90],
    tileSize: 256,
    resolutions: resolutions
  });

  var vectorSource = new ol.source.Vector();
  var layer = new ol.layer.Vector({ source: vectorSource, zIndex: 30 });

  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: fromLonLat([112, 32]), zoom: 3 }),
    controls: ol.control.defaults.defaults({ rotate: false })
  });

  var level = 4;

  function paint() {
    vectorSource.clear();

    /* 只画中国附近经纬度窗口内的瓦片,避免一次生成过多多边形 */
    var lonMin = 70, lonMax = 145, latMin = 0, latMax = 60;
    var span = 360 / (Math.pow(2, level) * 1.0);      /* x 方向每块度数 */
    var spanY = 180 / Math.pow(2, level);             /* y 方向每块度数 */

    for (var lat = Math.floor(latMin / spanY) * spanY; lat < latMax; lat += spanY) {
      for (var lon = Math.floor(lonMin / span) * span; lon < lonMax; lon += span) {
        var ring = [
          [lon, lat], [lon + span, lat], [lon + span, lat + spanY], [lon, lat + spanY]
        ].map(function (c) { return fromLonLat(c); });

        var tx = Math.round((lon + 180) / span);
        var ty = Math.round((90 - (lat + spanY)) / spanY);

        var f = new ol.Feature({ geometry: new ol.geom.Polygon([ring]) });
        var tint = (tx + ty) % 2 ? 0.10 : 0.04;
        f.setStyle(new ol.style.Style({
          fill: new ol.style.Fill({ color: 'rgba(31,95,208,' + tint + ')' }),
          stroke: new ol.style.Stroke({ color: 'rgba(31,95,208,0.55)', width: 1 }),
          text: new ol.style.Text({
            text: level + '/' + tx + '/' + ty,
            font: '500 10px ui-monospace,monospace',
            fill: new ol.style.Fill({ color: '#0b3d91' }),
            stroke: new ol.style.Stroke({ color: '#ffffff', width: 2.5 })
          })
        }));
        vectorSource.addFeature(f);
      }
    }

    document.getElementById('lv').textContent = level;
    document.getElementById('res').textContent = resolutions[level].toFixed(6);
    document.getElementById('span').textContent = span.toFixed(3);
  }

  document.getElementById('lvl').addEventListener('input', function () {
    level = Number(this.value);
    paint();
  });

  paint();
})();
</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 · EXAMPLEOgc Vector Tiles OpenLayers · EXAMPLEOgc Vector Tiles Geographic OpenLayers · EXAMPLEOsm Vector Tiles