JSFiddle - React, Tailwind, and code Playground

by ken3desu

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css">
<div id="map"></div>

CSS

#map {
    height: 500px;
    width: 80%;
}

JavaScript

// 地図
const osmUrl = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
const osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors';
const osm = L.tileLayer(osmUrl, {
  maxZoom: 18,
  attribution: osmAttrib,
});
// 初期化
const map = L.map('map').setView([34.6982856, 137.6999814], 12).addLayer(osm);

// 2点から四角になる様に4点の座標配列を生成
const makeBox = (x1, y1, x2, y2) => {
  return [
    [x1, y1],
    [x1, y2],
    [x2, y2],
    [x2, y1],
  ];
};
// 領域のセット
const HAMAMATSU_LU_LAT = 34.68;
const HAMAMATSU_LU_LNG = 137.65;
const HAMAMATSU_RD_LAT = 34.75;
const HAMAMATSU_RD_LNG = 137.73;
// 領域の外側を定義
const polygonLatLngList = [
  makeBox(-90, -180, 90, 180),// 世界全土を外枠
  makeBox(HAMAMATSU_LU_LAT, HAMAMATSU_LU_LNG, HAMAMATSU_RD_LAT, HAMAMATSU_RD_LNG),// 浜松の一部を内枠
];
// Polygon で領域外部を覆うドーナツ的なレイヤーを描画
L.polygon(polygonLatLngList, {
  color: 'black',
  opacity: 0, 
  fillOpacity: 0.6,
}).addTo(map);