Skip to main content

Live Update Feature

Change an existing feature on your map in real-time by updating its data. 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; }

            #map {
                position: absolute;
                top: 0;
                bottom: 0;
                width: 100%;
            }
        </style>
    </head>
    <body>
        <div id="map"></div>
        <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
        <script>
            TrimbleMaps.setAPIKey('YOUR_API_KEY_HERE');
            const map = new TrimbleMaps.Map({
            container: 'map',
            style: TrimbleMaps.Common.Style.TRANSPORTATION,
            zoom: 0,
      });

      map.on('load', () => {
        // We use D3 to fetch the JSON here so that we can parse and use it separately
        // from GL JS's use in the added source. You can use any request method (library
        // or otherwise) that you want.
        d3.json(
          'https://developer.trimblemaps.com/maps-sdk/assets/hike.geojson',
          (err, data) => {
            if (err) throw err;

            // save full coordinate list for later
            const coordinates = data.features[0].geometry.coordinates;

            // start by showing just the first coordinate
            data.features[0].geometry.coordinates = [coordinates[0]];

            // add it to the map
            map.addSource('trace', {type: 'geojson', data});
            map.addLayer({
              id: 'trace',
              type: 'line',
              source: 'trace',
              paint: {
                'line-color': 'yellow',
                'line-opacity': 0.75,
                'line-width': 5,
              },
            });

            // setup the viewport
            map.jumpTo({center: coordinates[0], zoom: 14});
            map.setPitch(30);

            // on a regular basis, add more coordinates from the saved list and update the map
            let i = 0;
            const timer = window.setInterval(() => {
              if (i < coordinates.length) {
                data.features[0].geometry.coordinates.push(coordinates[i]);
                map.getSource('trace').setData(data);
                map.panTo(coordinates[i]);
                i++;
              } else {
                window.clearInterval(timer);
              }
            }, 10);
          }
        );
      });
    </script>
  </body>
</html>
Last updated June 26, 2025.