Skip to main content

Get features under the mouse pointer

Use queryRenderedFeatures to show properties of hovered-over map elements.


<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Get features under the mouse pointer</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <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%;
      }
        #features {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 50%;
        overflow: auto;
        background: rgba(255, 255, 255, 0.8);
      }
      #map canvas {
        cursor: crosshair;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <div id="features"></div>
    <script>
      TrimbleMaps.setAPIKey('YOUR_API_KEY_HERE');

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

      map.on('mousemove', (e) => {
        const features = map.queryRenderedFeatures(e.point);

        // Limit the number of properties we're displaying for
        // legibility and performance
        const displayProperties = [
          'type',
          'properties',
          'id',
          'layer',
          'source',
          'sourceLayer',
          'state',
        ];

        const displayFeatures = features.map((feat) => {
          const displayFeat = {};
          displayProperties.forEach((prop) => {
            displayFeat[prop] = feat[prop];
          });
          return displayFeat;
        });

        document.getElementById('features').innerHTML = JSON.stringify(
          displayFeatures,
          null,
          2
        );
      });
    </script>
  </body>
</html>

Last updated June 26, 2025.