Leafletで表示したマップにジェスチャーハンドリングを追加する方法(Leaflet.GestureHandling)
GoogleMapをズームする際にPCだと「Ctrl+スクロール」、スマホだと「2本指でピンチアウト」という風にジェスチャーハンドリングがついており、この機能をLeafletのマップにも実装したい。「Leaflet.GestureHandling」というプラグインで実装できる模様。以下に対応方法をメモ。
Leaflet.GestureHandling
GitHub
https://github.com/elmarquis/Leaflet.GestureHandling/
デモページ
https://elmarquis.github.io/Leaflet.GestureHandling/examples/
CDN
CDNで使う場合は以下を記述する。
<link rel="stylesheet" href="https://unpkg.com/leaflet-gesture-handling/dist/leaflet-gesture-handling.min.css" type="text/css"> <script src="https://unpkg.com/leaflet-gesture-handling"></script>
サンプル
https://taitan916.info/sample/leaflet.js/plugin/leaflet-gesture-handling/
ソースコード
<div class="contents">サイトコンテンツ</div>
<div id="map"></div>
<div class="contents">サイトコンテンツ</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>
<link rel="stylesheet" href="https://unpkg.com/leaflet-gesture-handling/dist/leaflet-gesture-handling.min.css" type="text/css">
<script src="https://unpkg.com/leaflet-gesture-handling"></script>
<script>
let map;
map = L.map('map', {
gestureHandling: true, //ジェスチャーハンドリングを有効化
});
//中心の経度緯度
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);
</script>
オプションで「gestureHandling:true」を指定すると有効になる。
関連記事
-
-
leaflet.jsとOSMでマーカーの画像をそれぞれ指定する
leaflet.jsとOpenStreetMapでマップ上にマーカーを複数設置し ...
-
-
Leafletで「leaflet-search」プラグインを導入してマーカーの検索機能を追加する方法
LeafletとOpenStreetMapでマップ上にマーカーを複数設置した上で ...
-
-
Leafletで「leaflet.sprite」プラグインを導入してマーカーの色を変更する方法
LeafletとOpenStreetMapでマップ上にマーカーを複数設置したい。 ...
-
-
Leafletでマーカークリック時に当該マーカーをバウンド(跳ねる動作を)させる方法
LeafletとOpenStreetMapでマップ上にマーカーを複数設置した上で ...
-
-
Leafletで「leaflet-control-geocoder」プラグインを導入して住所検索機能を追加する方法
LeafletとOpenStreetMapでマップを表示させ、テキストボックスに ...