deck.gl Viewport Transition Example

Demo

Source

<html>
  <head>
    <title>deck.gl Viewport Transition Example</title>

    <script src="https://unpkg.com/deck.gl@^8.8.0/dist.min.js"></script>
    <script src="https://d3js.org/d3.v5.min.js"></script>
    <script src='https://unpkg.com/maplibre-gl@3.6.0/dist/maplibre-gl.js'></script>

    <link href='https://unpkg.com/maplibre-gl@3.6.0/dist/maplibre-gl.css' rel='stylesheet' />
    <style type="text/css">
      body {
        width: 100vw;
        height: 100vh;
        margin: 0;
      }

      #control-panel {
        font-size: 12px;
        font-family: Helvetica, Arial, sans-serif;
        position: absolute;
        top: 0;
        left: 0;
        margin: 12px;
        padding: 20px;
        line-height: 2;
        z-index: 1;
        background: #fff;
        border: solid 1px #ccc;
        border-bottom-color: #bbb;
        border-radius: 3px;
        box-shadow: 0 0 4px rgba(0, 0, 0, 0.15);
      }

    </style>
  </head>

  <body>
    <div id="control-panel">
    </div>
  </body>

  <script type="text/javascript">

    const {DeckGL, ScatterplotLayer, FlyToInterpolator} = deck;

    // Data
    const CITIES = [
      {"city":"San Francisco","state":"California","latitude":37.7751,"longitude":-122.4193},
      {"city":"New York","state":"New York","latitude":40.6643,"longitude":-73.9385},
      {"city":"Los Angeles","state":"California","latitude":34.051597,"longitude":-118.244263},
      {"city":"London","state":"United Kingdom","latitude":51.5074,"longitude":-0.1278},
      {"city":"Hyderabad","state":"India","latitude":17.3850,"longitude":78.4867}
    ];

    // Deck canvas
    const deckgl = new DeckGL({
      mapStyle: 'https://basemaps.cartocdn.com/gl/positron-nolabels-gl-style/style.json',
      initialViewState: {
        longitude: CITIES[0].longitude,
        latitude: CITIES[0].latitude,
        zoom: 10
      },
      controller: true,
      layers: [
        new ScatterplotLayer({
          data: CITIES,
          getPosition: d => [d.longitude, d.latitude],
          getColor: [255, 180, 0],
          radiusMinPixels: 10
        })
      ]
    });

    // Create radio buttons
    const inputs = d3.select('#control-panel').selectAll('div')
      .data(CITIES)
      .enter().append('div');

    inputs.append('input')
      .attr('type', 'radio')
      .attr('name', 'city')
      .attr('id', (d, i) => 'city-' + i)
      .on('change', d => {
        deckgl.setProps({
          initialViewState: {
            longitude: d.longitude,
            latitude: d.latitude,
            zoom: 10,
            transitionInterpolator: new FlyToInterpolator({speed: 1.5}),
            transitionDuration: 'auto'
          }
        })
      });

    inputs.append('label')
      .attr('for', (d, i) => 'city-' + i)
      .text(d => d.city + ', ' + d.state);

    // Default select the first city
    inputs.select('input').node().checked = true;

  </script>
</html>