dayjournal memo

Total 975 articles!!

Leaflet #026 – ミニマップ

Yasunori Kirimoto's avatar

Leafletでミニマップを作成するためには、「Leaflet.MiniMap」と言うプラグインを利用します。


index.html


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

<head>
    <meta charset="UTF-8">
    <title>Leaflet Sample</title>

    <script src="./library/leaflet-1.0.1/leaflet.js"></script>
    <link href="./library/leaflet-1.0.1/leaflet.css" rel="stylesheet" />

    <script src="http://maps.google.com/maps/api/js?sensor=false&amp;amp;region=JP"></script>
    <script src="./plugin/leaflet-plugins/layer/tile/Google.js"></script>

    <script src="./plugin/Leaflet-MiniMap/dist/Control.MiniMap.min.js"></script>
    <link href="./plugin/Leaflet-MiniMap/dist/Control.MiniMap.min.css" rel="stylesheet" />

    <link href="./css/stylesheet.css" rel="stylesheet" />

</head>
<body>

    <div id="map"></div>
    <script src="./js/script.js"></script>

</body>
</html>

stylesheet.css


html, body {
    height: 100%;
    padding: 0;
    margin: 0;
}

#map {
    z-index: 0;
    height: 100%;
}

script.js


var t_std = new L.tileLayer('http://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png', {
    attribution: "<a href='http://www.gsi.go.jp/kikakuchousei/kikakuchousei40182.html' target='_blank'>国土地理院</a>"
});

var t_pale = new L.tileLayer('http://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png', {
    attribution: "<a href='http://www.gsi.go.jp/kikakuchousei/kikakuchousei40182.html' target='_blank'>国土地理院</a>"
});

var t_ort = new L.tileLayer('http://cyberjapandata.gsi.go.jp/xyz/ort/{z}/{x}/{y}.jpg', {
    attribution: "<a href='http://www.gsi.go.jp/kikakuchousei/kikakuchousei40182.html' target='_blank'>国土地理院</a>"
});

var o_std = new L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&amp;copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
});

var o_std_mini = new L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&amp;copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
});

var g_roadmap = new L.Google('ROADMAP');

var g_satellite = new L.Google('SATELLITE');

var g_hybrid = new L.Google('HYBRID');

var map = L.map('map', {
    center: [35.6831925, 139.7511307],
    zoom: 13,
    zoomControl: true,
    layers: [o_std]
});

var MiniMap = new L.Control.MiniMap(o_std_mini).addTo(map);

var Map_BaseLayer = {
    "地理院地図 標準": t_std,
    "地理院地図 淡色": t_pale,
    "地理院地図 オルソ": t_ort,
    "OpenStreetMap 標準": o_std,
    "GoogleMap 標準": g_roadmap,
    "GoogleMap オルソ": g_satellite,
    "GoogleMap ハイブリッド": g_hybrid
};

L.control.scale({
    imperial: false,
    maxWidth: 300
}).addTo(map);

L.control.layers(Map_BaseLayer, null, {
    collapsed: false
}).addTo(map)

index.htmlを実行すると下記のようにブラウザで表示されます。 leaflet_026_01


zoomレベル変更:


var MiniMap = new L.Control.MiniMap(
                        o_std_mini,
                        {zoomLevelOffset: -7}
                    ).addTo(map)

閉じるボタン追加:


var MiniMap = new L.Control.MiniMap(
                        o_std_mini,
                        {toggleDisplay: true}
                    ).addTo(map)

ミニマップの機能を実装したい時に便利なプラグインです。



book

Q&A