TomTom Maps for JavaScript
    Preparing search index...

    Function bboxFromGeoJSON

    • Extracts or calculates a bounding box from GeoJSON objects.

      This utility function handles various GeoJSON types and automatically determines the best approach to obtain a bounding box:

      • Uses existing bbox properties when available (fastest)
      • Calculates from geometry coordinates when needed
      • Aggregates bounding boxes from collections
      • Optimizes large geometries by sampling points for performance

      The function prioritizes existing bbox fields over geometry calculations, which is important for Point features from TomTom services that may have bbox representing a broader area than just the point location.

      Parameters

      • hasBBox: HasBBox

        A GeoJSON object (Feature, FeatureCollection, Geometry, etc.) or array of such objects

      Returns OptionalBBox

      The bounding box as [minLng, minLat, maxLng, maxLat], or undefined if input is invalid

      // From a Feature with existing bbox
      const place = await geocode({ key: 'key', query: 'Amsterdam' });
      const bbox = bboxFromGeoJSON(place);
      // Returns the bbox that came with the place

      // From a Polygon geometry (calculates bbox)
      const polygon = {
      type: 'Polygon',
      coordinates: [[
      [4.88, 52.36],
      [4.90, 52.36],
      [4.90, 52.38],
      [4.88, 52.38],
      [4.88, 52.36]
      ]]
      };
      const polyBbox = bboxFromGeoJSON(polygon);
      // Returns: [4.88, 52.36, 4.90, 52.38]

      // From a FeatureCollection (aggregates all features)
      const places = await search({ key: 'key', query: 'coffee' });
      const collectionBbox = bboxFromGeoJSON(places);
      // Returns bbox encompassing all search results

      // From a LineString (calculates from coordinates)
      const route = await calculateRoute({
      key: 'key',
      locations: [[4.9, 52.3], [4.5, 51.9]]
      });
      const routeBbox = bboxFromGeoJSON(route.routes[0].geometry);
      // Returns bbox containing the entire route

      // From an array of GeoJSON objects
      const multiBbox = bboxFromGeoJSON([place1, place2, place3]);
      // Returns bbox encompassing all three places