dayjournal memo

Total 973 articles!!

Mapbox GL JS #031 – マップ内のスタイル情報取得

Yasunori Kirimoto's avatar


画像



マップ内のスタイル情報を取得するメモ。



画像



script.js

// MIERUNE MONO読み込み
let map = new mapboxgl.Map({
    container: "map",
    style: {
        "version": 8,
        "sources": {
            "MIERUNEMAP": {
                "type": "raster",
                "tiles": ["https://tile.mierune.co.jp/mierune_mono/{z}/{x}/{y}.png"],
                "tileSize": 256,
                "attribution": "Maptiles by <a href='http://mierune.co.jp/' target='_blank'>MIERUNE</a>, under CC BY. Data by <a href='http://osm.org/copyright' target='_blank'>OpenStreetMap</a> contributors, under ODbL."
            }
        },
        "layers": [{
            "id": "MIERUNEMAP",
            "type": "raster",
            "source": "MIERUNEMAP",
            "minzoom": 10,
            "maxzoom": 18
        }]
    },
    center: [139.767, 35.681],
    zoom: 11
});

// コントロール関係表示
map.addControl(new mapboxgl.NavigationControl());

map.on('load', function () {
    // サークル設定
    map.addSource('point_sample', {
        type: 'geojson',
        data: {
            "type": "FeatureCollection",
            "name": "sample",
            "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
            "features": [
                { "type": "Feature", "properties": { "field01": "一", "field02": "abcd", "field03": 5 }, "geometry": { "type": "Point", "coordinates": [ 139.766778945922852, 35.68198003744061 ] } },
                { "type": "Feature", "properties": { "field01": "二", "field02": null, "field03": 7 }, "geometry": { "type": "Point", "coordinates": [ 139.762916564941406, 35.674310750817348 ] } },
                { "type": "Feature", "properties": { "field01": "三", "field02": "kojsha", "field03": 9 }, "geometry": { "type": "Point", "coordinates": [ 139.763603210449219, 35.691391336319306 ] } }
            ]
        }
    });

    // スタイル設定
    map.addLayer({
        "id": "point_sample",
        "type": "circle",
        "source": "point_sample",
        "layout": {},
        "paint": {
            'circle-color': "#FF0000",
            'circle-radius': 10
        }
    });

    // マップ内のスタイル情報取得
    const styleJson = map.getStyle();
    console.log(styleJson);
});



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



book

Q&A