Skip to main content

Fit a map to a bounding box

Fit the map to a specific area, regardless of the pixel size of the map. Requires Trimble Maps v3.0.0 or later.


<!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%; overflow: hidden; }
            #fit {
              display: block;
              position: absolute;
              top: 20px;
              left: 50%;
              transform: translate(-50%);
              width: 200px;
              height: 40px;
              padding: 10px;
              border: none;
              border-radius: 4px;
              font-size: 12px;
              text-align: center;
              color: #fff;
              background: #ee8a65;
           }
        </style>
    </head>
    <body>
        <div id="map"></div>
         <br>
    <button type="button" id="fit">Fit to Kenya</button>

        <script>
            TrimbleMaps.setAPIKey('YOUR_API_KEY_HERE');
            const map = new TrimbleMaps.Map({
              container: 'map',
              style: TrimbleMaps.Common.Style.TRANSPORTATION,
              center: [-74.5, 40],
              zoom: 9,
         });

      document.getElementById('fit').addEventListener('click', () => {
        map.fitBounds([
          [32.958984, -5.353521],
          [43.50585, 5.615985],
        ]);
      });
    </script>
  </body>
</html>
Last updated June 26, 2025.