LoginSignup
11
4

More than 5 years have passed since last update.

Mapboxで建物の3D表示ができるみたいなので、軽く触ってみた

Last updated at Posted at 2016-10-23

Mapboxで建物の3D表示ができるみたいなので、軽く触ってみました\(^o^)/
※建物の3D表示については、こちらの記事に詳しく書かれています。

3D描画

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Display buildings in 3D</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <script src="https://api.tiles.mapbox.com/mapbox-gl-js/v0.26.0/mapbox-gl.js"></script>
    <link href="https://api.tiles.mapbox.com/mapbox-gl-js/v0.26.0/mapbox-gl.css" rel="stylesheet" />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
    </style>
</head>
<body>

<div id="map"></div>
<script>
mapboxgl.accessToken = 'APIとーくん';
var map = new mapboxgl.Map({
    style: 'mapbox://styles/mapbox/light-v9',
    center: [139.766247, 35.681298],
    zoom: 16,
    pitch: 45,
    bearing: -17.6,
    container: 'map'
});

map.on('load', function() {
    map.addLayer({
        'id': '3d-buildings',
        'source': 'composite',
        'source-layer': 'building',
        'filter': ['==', 'extrude', 'true'],
        'type': 'fill',
        'minzoom': 16,
        'paint': {
            'fill-color': '#42A5F5',
            'fill-extrude-height': {
                'type': 'identity',
                'property': 'height'
            },
            'fill-extrude-base': {
                'type': 'identity',
                'property': 'min_height'
            },
            'fill-opacity': .6
        }
    });

});
</script>

</body>
</html>

agagas.png

色の変更

建物の色は、paintfill-colorを設定すると変更できます。

 'fill-color': '#42A5F5'

光の調整

全体の光の調整とかは、setLightを使うと変更できます。

map.on('load', function() {
    map.setLight({color: "#42A5F5", intensity: 0.8, position: [1.15, 135, 145]});
    map.addLayer({
        'id': '3d-buildings',
        //以下略

screencapture-localhost-3000-1477228177506.png

まとめ

APIについては、こちらに詳しく書かれています。
https://www.mapbox.com/mapbox-gl-js/api/

あ! 
Mapboxはnpmやyarnでも入れることができます(^O^)v

npm install --save mapbox-gl
yarn add mapbox-gl
11
4
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
11
4