0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Mapbox GL JS でアトリビューションをコントロールする -利用編-

Last updated at Posted at 2021-04-03

調査編につづき、今回は実際にユーザーコードでAttributionをコントロールしてみます。なお、SDKとしての挙動を試す目的でAttributionの表示をコントロールしますが、実際には利用許諾にしたがった表示を行ってください。

Text attribution

Attributionを非表示

options.attributionControlfalseに設定するとtext attributionが非表示になります。

    var map = new mapboxgl.Map({
        container: 'map', // container id
        style: 'mapbox://styles/mapbox/streets-v11', // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        zoom: 9, // starting zoom
        attributionControl: false
    });

off.png

カスタムAttributionを表示

options.attributionControlに文字列、もしくは文字列の配列を指定するとAttributionとして表示されます。

    var map = new mapboxgl.Map({
        container: 'map', // container id
        style: 'mapbox://styles/mapbox/streets-v11', // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        zoom: 9, // starting zoom
        customAttribution: '<a href="https://example.com/custom1">custom1</a>'
    });

custom1.png

    var map = new mapboxgl.Map({
        container: 'map', // container id
        style: 'mapbox://styles/mapbox/streets-v11', // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        zoom: 9, // starting zoom
        customAttribution: ['<a href="https://example.com/custom1">custom1</a>', 'custom2']
    });

custom2.png

デフォルトのAttributionを消した上でカスタムAttributionを追加(compact表示設定)

    var map = new mapboxgl.Map({
        container: 'map', // container id
        style: 'mapbox://styles/mapbox/streets-v11', // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        zoom: 9, // starting zoom
        attributionControl: false
    });

    map.addControl(new mapboxgl.AttributionControl({
        compact: true,
        customAttribution: '<a href="https://example.com/custom1">custom1</a>'}));

初期表示
compact1.png

展開表示
compact2.png

StyleでAttributionを設定

urlでアクセス

    var map = new mapboxgl.Map({
        container: 'map', // container id
        center: [-74.5, 40], // starting position [lng, lat]
        style: {
            version: 8,
            sources: {
                street: {
                    type: 'vector',
                    url: 'mapbox://mapbox.mapbox-streets-v8'
                }
            },
            layers: [
                {
                    id: 'water',
                    type: 'fill',
                    source: 'street',
                    'source-layer': 'water',
                    minzoom: 0,
                    maxzoom: 22
                }
            ]
        },
        zoom: 9 // starting zoom
    });

style_url.png

urlでアクセス + Attributionを指定

    var map = new mapboxgl.Map({
        container: 'map', // container id
        center: [-74.5, 40], // starting position [lng, lat]
        style: {
            version: 8,
            sources: {
                street: {
                    type: 'vector',
                    url: 'mapbox://mapbox.mapbox-streets-v8',
                    attribution: 'in style',
                }
            },
            layers: [
                {
                    id: 'water',
                    type: 'fill',
                    source: 'street',
                    'source-layer': 'water',
                    minzoom: 0,
                    maxzoom: 22
                }
            ]
        },
        zoom: 9 // starting zoom
    });

style_url_attr.png

tilesでアクセス


    var map = new mapboxgl.Map({
        container: 'map', // container id
        //style: 'mapbox://styles/mapbox/streets-v11', // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        style: {
            version: 8,
            sources: {
                street: {
                    type: 'vector',
                    tiles: ['http://a.tiles.mapbox.com/v4/mapbox.mapbox-streets-v8/{z}/{x}/{y}.vector.pbf?access_token=pk.eyJ1IjoieW9jaGkiLCJhIjoiY2tjZThvdWExMDV2dDJxcDgxZzBwbzlxYSJ9.M0yRA6SXDMRgXzXGuYnvsg'],
                    tileSize: 512
                }
            },
            layers: [
                {
                    id: 'water',
                    type: 'fill',
                    source: 'street',
                    'source-layer': 'water',
                    minzoom: 0,
                    maxzoom: 22
                }
            ]
        },
        zoom: 9 // starting zoom
    });

style_tiles.png

tilesでアクセス + Attributionを指定


    var map = new mapboxgl.Map({
        container: 'map', // container id
        //style: 'mapbox://styles/mapbox/streets-v11', // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        style: {
            version: 8,
            sources: {
                street: {
                    type: 'vector',
                    tiles: ['http://a.tiles.mapbox.com/v4/mapbox.mapbox-streets-v8/{z}/{x}/{y}.vector.pbf?access_token=pk.eyJ1IjoieW9jaGkiLCJhIjoiY2tjZThvdWExMDV2dDJxcDgxZzBwbzlxYSJ9.M0yRA6SXDMRgXzXGuYnvsg'],
                    tileSize: 512,
                    attribution: 'in style'
                }
            },
            layers: [
                {
                    id: 'water',
                    type: 'fill',
                    source: 'street',
                    'source-layer': 'water',
                    minzoom: 0,
                    maxzoom: 22
                }
            ]
        },
        zoom: 9 // starting zoom
    });

style_tiles_attr.png

Workmark

urlでアクセスし、wordmarkを非表示


    var map = new mapboxgl.Map({
        container: 'map', // container id
        //style: 'mapbox://styles/mapbox/streets-v11', // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        style: {
            version: 8,
            sources: {
                street: {
                    type: 'vector',
                    url: 'mapbox://mapbox.mapbox-streets-v8',
                    mapbox_logo: false
                }
            },
            layers: [
                {
                    id: 'water',
                    type: 'fill',
                    source: 'street',
                    'source-layer': 'water',
                    minzoom: 0,
                    maxzoom: 22
                }
            ]
        },
        zoom: 9 // starting zoom
    });

logo_url.png

tilesでアクセスし、wordmarkを表示


    var map = new mapboxgl.Map({
        container: 'map', // container id
        //style: 'mapbox://styles/mapbox/streets-v11', // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        style: {
            version: 8,
            sources: {
                street: {
                    type: 'vector',
                    tiles: ['http://a.tiles.mapbox.com/v4/mapbox.mapbox-streets-v8/{z}/{x}/{y}.vector.pbf?access_token=pk.eyJ1IjoieW9jaGkiLCJhIjoiY2tjZThvdWExMDV2dDJxcDgxZzBwbzlxYSJ9.M0yRA6SXDMRgXzXGuYnvsg'],
                    tileSize: 512,
                    mapbox_logo: true
                }
            },
            layers: [
                {
                    id: 'water',
                    type: 'fill',
                    source: 'street',
                    'source-layer': 'water',
                    minzoom: 0,
                    maxzoom: 22
                }
            ]
        },
        zoom: 9 // starting zoom
    });

logo_tiles.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?