Skip to main content

Features Under Mouse Pointer

Get the features under a mouse pointer.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <link rel="stylesheet" href="https://maps-sdk.trimblemaps.com/v3/trimblemaps-3.21.0.css" />
  <script src="https://maps-sdk.trimblemaps.com/v3/trimblemaps-3.21.0.js"></script>
  <style>
  body {
      margin: 0;
      padding: 0;
  }

  #map {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 50%;
  }

  #features {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      width: 50%;
      overflow: auto;
      background: rgba(255, 255, 255, 0.8);
  }
  </style>
</head>
<body>
  <div id="map"></div>
  <pre id="features"></pre>
    <script>
            TrimbleMaps.APIKey = 'YOUR_API_KEY_HERE';
            const map = new TrimbleMaps.Map({
            container: 'map', // container id
            style: TrimbleMaps.Common.Style.TRANSPORTATION, //hosted style id
            center: [-80.00633,40.44888], // starting position
            zoom: 14 // starting zoom
            });

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

                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 15, 2023.