iT邦幫忙

2022 iThome 鐵人賽

DAY 28
0
Modern Web

vue3 + Leaflet.js系列 第 28

day28 Extending L.GridLayer 和 DOM

  • 分享至 

  • xImage
  •  

昨天介紹 L.TileLayer 建立圖層,今天要介紹另一種方式 L.GridLayer

  • L.GridLayer.DebugCoords : 建立網格座標
  • L.gridLayer.debugCoords : 接收網格座標資料
L.GridLayer.DebugCoords = L.GridLayer.extend({
  createTile: function (coords) {
    var tile = document.createElement("div");
    tile.innerHTML = [coords.x, coords.y, coords.z].join(", ");
    tile.style.outline = "1px solid red";
    return tile;
  },
});

L.gridLayer.debugCoords = function (opts) {
  return new L.GridLayer.DebugCoords(opts);
};

onMounted(() => {
  map = new L.Map(mapContent.value, {
    center: [23.695, 121.102],
    zoom: 8,
  });

  map.addLayer(L.gridLayer.debugCoords());
});

參考資料

https://leafletjs.com/examples/extending/extending-2-layers.html

完整程式碼

<template>
  <!-- map -->
  <div class="h-[100vh]" ref="mapContent"></div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import L from "leaflet";
import "leaflet/dist/leaflet.css";

let map = {};
const mapContent = ref(null);

const osmUrl = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
const attribution = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>';

L.GridLayer.DebugCoords = L.GridLayer.extend({
  createTile: function (coords) {
    var tile = document.createElement("div");
    tile.innerHTML = [coords.x, coords.y, coords.z].join(", ");
    tile.style.outline = "1px solid red";
    return tile;
  },
});

L.gridLayer.debugCoords = function (opts) {
  return new L.GridLayer.DebugCoords(opts);
};

onMounted(() => {
  map = new L.Map(mapContent.value, {
    center: [23.695, 121.102],
    zoom: 8,
  });

  map.addLayer(L.gridLayer.debugCoords());
});
</script>


上一篇
day27 Extending Leaflet Layers
下一篇
day29 Extending Leaflet onAdd 和 onRemove
系列文
vue3 + Leaflet.js30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言