leaflet 显示地图加载的瓦片的行列号

背景:

在开发过程中,对接wmts服务的时候,调试参数过程中有时候需要直观看到当前地图加载的瓦片的行列号。

实现原理:

利用Leaflet的L.GridLayer图层,加载一个网格图层,重写其createTile方法,来专门显示瓦片的行列号。

代码实现

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.min.js"></script>
  <link href="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.min.css" rel="stylesheet">
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    html,
    body,
    #map {
      height: 100%;
      width: 100%;
      overflow: hidden;
    }
  </style>
</head>

<body>
  <div id="map"></div>
  <script>
    let map;
    (function () {
      let tdtLayer, gridLayer;
      const TK_KEY = '天地图key';
      L.CRS.CustomEPSG4326 = L.extend({}, L.CRS.Earth, {
        code: 'EPSG:4326',
        projection: L.Projection.LonLat,
        transformation: new L.Transformation(1 / 180, 1, -1 / 180, 0.5),
        scale: function (zoom) {
          return 256 * Math.pow(2, zoom - 1);
        }
      });
      map = L.map('map', {
        preferCanvas: true,
        crs: L.CRS.CustomEPSG4326,
        minZoom: 0,
        maxZoom: 20,
        center: [29.563761, 106.550464],
        zoom: 15,
      });

      initTdtImgLayers()
      function initTdtImgLayers() {
        const img_c = `http://t1.tianditu.com/img_c/wmts?layer=img&style=default&tilematrixset=c&Service=WMTS&Request=GetTile&Version=1.0.0&Format=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&tk=${TK_KEY}`;
        debugger
        tdtLayer = L.tileLayer(img_c, {
          tileSize: 256,
          attribution: false,
          subdomains: [0, 1, 2],
        });
        tdtLayer.on('add', () => {
           initGridLayer()
        });
        tdtLayer.addTo(map)
      }

      function initGridLayer() {
        gridLayer = new L.GridLayer({
          attribution: 'Grid Layer',
          tileSize: new L.Point(256, 256)
        });
        gridLayer.createTile = (coords) => {
          const tile = L.DomUtil.create('div', 'grid');
          const indexStr = [coords.x, coords.y, coords.z].join(', ');
          tile.style.outline = '1px solid red';
          tile.style.color = 'white';
          tile.innerHTML = indexStr;
          return tile;
        };
        gridLayer.addTo(map);
      }
    })()

  </script>
</body>

</html>

效果

在这里插入图片描述

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-04-13 05:48:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-13 05:48:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-13 05:48:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-13 05:48:02       18 阅读

热门阅读

  1. 什么是跨域?

    2024-04-13 05:48:02       14 阅读
  2. LinuxShell编程中source和export命令

    2024-04-13 05:48:02       17 阅读
  3. 第八十二周周报

    2024-04-13 05:48:02       18 阅读
  4. C++利用缓冲区来提高读写文件的效率

    2024-04-13 05:48:02       12 阅读
  5. SpringBoot初始化工程

    2024-04-13 05:48:02       17 阅读
  6. 常见的分类算法介绍

    2024-04-13 05:48:02       17 阅读
  7. 图论中的模板

    2024-04-13 05:48:02       16 阅读
  8. LeetCode:1两数之和 C语言

    2024-04-13 05:48:02       10 阅读