dayjournal memo

Total 975 articles!!

Mapbox GL JS #016 – ラスタタイル背景を切り替え表示

Yasunori Kirimoto's avatar


画像



ラスタタイル背景を切り替え表示するメモ。



画像



Mapbox GL JSでは、Leafletのようなレイヤ切り替え機能は存在しません。機能を実装するためには、独自で構築する必要があります。

今回は、このサンプルを参考に構築しました。



index.html


<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MapboxGLJS Starter</title>
</head>
<body>
    <nav id="menu"></nav>
    <div id="map"></div>
    <script src="./app.js"></script>
</body>
</html>


style.css


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

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

#menu {
    background: #fff;
    position: absolute;
    z-index: 1;
    top: 10px;
    right: 10px;
    border-radius: 3px;
    width: 180px;
    border: 1px solid rgba(0,0,0,0.4);
}

#menu a {
    font-size: 13px;
    color: #404040;
    display: block;
    margin: 0;
    padding: 10px;
    text-decoration: none;
    border-bottom: 1px solid rgba(0,0,0,0.25);
    text-align: center;
}

#menu a:last-child {
    border: none;
}

#menu a:hover {
    background-color: #f8f8f8;
    color: #404040;
}

#menu a.active {
    background-color: #8DCF3F;
    color: #ffffff;
}

#menu a.active:hover {
    background: #79bb2b;
}


script.js


// MIERUNE MONO読み込み
var map = new mapboxgl.Map({
    container: "map",
    style: {
        version: 8,
        sources: {
            m_mono: {
                type: "raster",
                tiles: ["https://tile.mierune.co.jp/mierune_mono/{z}/{x}/{y}.png"],
                tileSize: 256
            }
        },
        layers: [{
            id: "m_mono",
            type: "raster",
            source: "m_mono",
            minzoom: 0,
            maxzoom: 18
        }]
    },
    center: [139.767, 35.681],
    zoom: 11
});


map.on("load", function() {
    
    // MIERUNE Color読み込み
    map.addSource("m_color", {
        type: "raster",
        tiles: ["https://tile.mierune.co.jp/mierune/{z}/{x}/{y}.png"],
        tileSize: 256
    });
    map.addLayer({
        id: "m_color",
        type: "raster",
        source: "m_color",
        minzoom: 0,
        maxzoom: 18
    });

    // 地理院タイル 淡色読み込み
    map.addSource("t_pale", {
        type: "raster",
        tiles: ["http://cyberjapandata.gsi.go.jp/xyz/pale/{z}/{x}/{y}.png"],
        tileSize: 256
    });
    map.addLayer({
        id: "t_pale",
        type: "raster",
        source: "t_pale",
        minzoom: 0,
        maxzoom: 18
    });

    // 地理院タイル オルソ読み込み
    map.addSource("t_ort", {
        type: "raster",
        tiles: ["http://cyberjapandata.gsi.go.jp/xyz/ort/{z}/{x}/{y}.jpg"],
        tileSize: 256
    });
    map.addLayer({
        id: "t_ort",
        type: "raster",
        source: "t_ort",
        minzoom: 0,
        maxzoom: 18
    });

    // OpenStreetMap読み込み
    map.addSource("o_std", {
        type: "raster",
        tiles: [
            "https://a.tile.openstreetmap.org/{z}/{x}/{y}.png",
            "https://b.tile.openstreetmap.org/{z}/{x}/{y}.png"
        ],
        tileSize: 256
    });
    map.addLayer({
        id: "o_std",
        type: "raster",
        source: "o_std",
        minzoom: 0,
        maxzoom: 18
    });


    // レイヤ設定
    var Map_BaseLayer = {
        m_mono: "MIERUNE MONO",
        m_color: "MIERUNE Color",
        t_pale: "地理院タイル 淡色",
        t_ort: "地理院タイル オルソ",
        o_std: "OpenStreetMap"
    };


    // レイヤメニュー作成
    for (var i = 0; i < Object.keys(Map_BaseLayer).length; i++) {
        // レイヤID取得
        var id = Object.keys(Map_BaseLayer)[i];
        // aタグ作成
        var link = document.createElement("a");
        link.href = "#";
        // id追加
        link.id = id;
        // 名称追加
        link.textContent = Map_BaseLayer[id];

        // 初期表示m_mono以外非表示
        if (id === "m_mono") {
            link.className = "active";
        } else {
            map.setLayoutProperty(id, "visibility", "none");
            link.className = "";
        }

        //aタグクリック処理
        link.onclick = function (e) {
            // id取得
            var clickedLayer = this.id;
            e.preventDefault();
            e.stopPropagation();

            // ON/OFF状態取得
            var visibility = map.getLayoutProperty(clickedLayer, "visibility");

            // ON/OFF判断
            if (visibility === "visible") {
            } else {
                for (var j = 0; j < Object.keys(Map_BaseLayer).length; j++) {
                    // レイヤID取得
                    var ch_id = Object.keys(Map_BaseLayer)[j];

                    // レイヤの表示・非表示
                    if (ch_id === clickedLayer) {
                        // クリックしたレイヤを表示
                        this.className = "active";
                        map.setLayoutProperty(clickedLayer, "visibility", "visible");
                    } else {
                        // クリックしたレイヤ以外を非表示
                        var ch_obj = document.getElementById(ch_id);
                        ch_obj.className = "";
                        map.setLayoutProperty(ch_id, "visibility", "none");
                    }
                }
            }
        };

        // レイヤメニューにレイヤ追加
        var layers = document.getElementById("menu");
        layers.appendChild(link);
    }

});


// ズームコントロール
var nc = new mapboxgl.NavigationControl();
map.addControl(nc, 'top-left');



Mapbox GL JSを手軽に始めるビルド環境公開しています。
mapboxgljs-starter



book

Q&A