Leafletにて表示されているマップを画像としてダウンロードさせる「leaflet-easyPrint」の利用方法
Leafletにて何らかのボタンをクリックすると表示されているマップを画像としてダウンロードさせたい。調べたところ「leaflet-easyPrint」というプラグインで簡単に実装できた。以下にサンプルと実装方法をメモ。
leaflet-easyPrint
GitHub
https://github.com/rowanwins/leaflet-easyPrint
デモページ
http://rowanwins.github.io/leaflet-easyPrint/
CDN
CDNで使う場合は以下を記述する。
<script src="https://cdn.jsdelivr.net/npm/leaflet-easyprint@2.1.9/dist/bundle.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/leaflet-easyprint@2.1.9/libs/leaflet.min.css" rel="stylesheet">
サンプル
https://taitan916.info/sample/leaflet.js/plugin/leaflet-easyPrint/
マップ左上の保存ボタン→サイズを選択すると画像がダウンロードされる筈。
実装方法
ソースコード
<div id="map" class="sidebar-map"></div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.js"></script>
<script src="https://cdn.jsdelivr.net/npm/leaflet-easyprint@2.1.9/dist/bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/leaflet-easyprint@2.1.9/libs/leaflet.min.css" rel="stylesheet">
<script>
let map;
map = L.map('map');
//中心の経度緯度
const center = ['35.681236', '139.767125'];
//ズームレベル
const zoom = 12;
//マップの中心地とズームレベルを指定
map.setView(center, zoom);
const tileLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 19
});
tileLayer.addTo(map);
//プラグインのオプション設定
const option = {
sizeModes: ['Current', 'A4Landscape', 'A4Portrait'],
filename: 'テストマップ',
exportOnly: true,
hideControlContainer: true
};
//プラグイン実行
const printer = L.easyPrint(option).addTo(map);
</script>
オプション設定について
オプション項目についてはこちらのページを参照すること。
関連記事
-
-
Leafletでマップ上に通知(トースト)機能を追加する方法(Leaflet.Notifications)
Leafletでマーカー内のボタンクリック時等、ユーザーが何らかのアクションを行 ...
-
-
Leafletでマーカーをグループ化し、チェックボックスの状態により表示非表示を切り替える方法
LeafletとOpenStreetMapでマップ上にマーカーを複数設置した上で ...
-
-
Leafletで「leaflet.sprite」プラグインを導入してマーカーの色を変更する方法
LeafletとOpenStreetMapでマップ上にマーカーを複数設置したい。 ...
-
-
Leafletにて操作内容を履歴に残し「進む」「戻る」ボタンで履歴を実行できる「Leaflet.NavBar」の利用方法
Leafletにてマップ移動やズーム等、ユーザーの操作内容を履歴に残して、「進む ...
-
-
Leafletで「sidebar-v2」プラグインを導入してマップ上にサイドバーを設置する方法
Leafletでマップ上にサイドバー及びサイドメニューを実装したい。調べたところ ...