Skip to main content

Fit to the bounds of a LineString

Get the bounds of a LineString.

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="https://maps-sdk.trimblemaps.com/v4/trimblemaps-4.2.3.css" />
        <script src="https://maps-sdk.trimblemaps.com/v4/trimblemaps-4.2.3.js"></script>
    <style>
      body {
        margin: 0;
        padding: 0;
      }
      html,
      body,
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <style>
      .btn-control {
        background-color: #3386c0;
        border-radius: 3px;
        border: none;
        color: #fff;
        cursor: pointer;
        display: block;
        font: bold 12px/20px system-ui, sans-serif;
        left: 50%;
        margin-left: -100px;
        padding: 10px 20px;
        position: absolute;
        top: 20px;
        width: 200px;
        z-index: 1;
      }

      .btn-control:hover {
        background-color: #4ea0da;
      }
    </style>
    <div id="map"></div>
    <button id="zoomto" class="btn-control">Zoom to bounds</button>

    <script>
      // A GeoJSON object with a LineString route from the White House to Capitol Hill
      const geojson = {
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            geometry: {
              type: 'LineString',
              properties: {},
              coordinates: [
                [-77.0366048812866, 38.89873175227713],
                [-77.03364372253417, 38.89876515143842],
                [-77.03364372253417, 38.89549195896866],
                [-77.02982425689697, 38.89549195896866],
                [-77.02400922775269, 38.89387200688839],
                [-77.01519012451172, 38.891416957534204],
                [-77.01521158218382, 38.892068305429156],
                [-77.00813055038452, 38.892051604275686],
                [-77.00832366943358, 38.89143365883688],
                [-77.00818419456482, 38.89082405874451],
                [-77.00815200805664, 38.88989712255097],
              ],
            },
          },
        ],
      };

      TrimbleMaps.setAPIKey('YOUR_API_KEY_HERE');

      const map = new TrimbleMaps.Map({
        container: 'map',
        style: TrimbleMaps.Common.Style.TRANSPORTATION,
        center: [-77.0214, 38.897],
        zoom: 12,
      });

      map.on('load', () => {
        map.addSource('LineString', {
          type: 'geojson',
          data: geojson,
        });
        map.addLayer({
          id: 'LineString',
          type: 'line',
          source: 'LineString',
          layout: {
            'line-join': 'round',
            'line-cap': 'round',
          },
          paint: {
            'line-color': '#BF93E4',
            'line-width': 5,
          },
        });

        document.getElementById('zoomto').addEventListener('click', () => {
          // Geographic coordinates of the LineString
          const coordinates = geojson.features[0].geometry.coordinates;

          // Pass the first coordinates in the LineString to `lngLatBounds` &
          // wrap each coordinate pair in `extend` to include them in the bounds
          // result. A variation of this technique could be applied to zooming
          // to the bounds of multiple Points or Polygon geometries - it just
          // requires wrapping all the coordinates with the extend method.
          const bounds = coordinates.reduce((bounds, coord) => {
            return bounds.extend(coord);
          }, new TrimbleMaps.LngLatBounds(coordinates[0], coordinates[0]));

          map.fitBounds(bounds, {
            padding: 20,
          });
        });
      });
    </script>
  </body>
</html>

Last updated June 26, 2025.