Search code examples
javascriptsdkreporttrimble-maps

How to get reports from a Routing Instance in Trimble Maps JavaScript Maps SDK?


I would like to get mileage data using Trimble Maps Routing. The getReports() method in a Routing instance should provide this information. However, anytime i call this method i receive null. How can i get mile reports from either TrimbleMaps.Map or TrimbleMaps.Route instances?

documentation: https://developer.trimblemaps.com/maps-sdk/api/#route

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <link href="https://maps-sdk.trimblemaps.com/v2/trimblemaps-2.1.1.css" rel="stylesheet">
</head>
<body>

<div id="map" style="height: 600px; width: 800px;"></div>

<script src="https://maps-sdk.trimblemaps.com/v2/trimblemaps-2.1.1.js"></script>
<script>

  TrimbleMaps.APIKey = "17CA0885B03A6B4FADBDC3D1A51DC0BD";

  let map = new TrimbleMaps.Map({
    container: "map",
    center: new TrimbleMaps.LngLat(-80.1709, 25.7745),
    zoom: 10
  });

  map.on('load', () => {
    let myRoute = new TrimbleMaps.Route({
      routeId: 'myRoute',
      stops: [
        new TrimbleMaps.LngLat(-80.35564219999999, 25.7632547),
        new TrimbleMaps.LngLat(-80.20527899999999, 25.8180518)
      ],
      reportType: [TrimbleMaps.Common.ReportType.MILEAGE]
    });
    myRoute.addTo(map);
    console.log(myRoute.getReports()); // undefined
  });

</script>

</body>
</html>

Solution

  • For the javascript API Trimble Maps.

    myRoute.getReports() function has a listener do the following:

    const myRoute = new TrimbleMaps.Route({
      routeId: "myRoute",
      stops: [
          new TrimbleMaps.LngLat(-74.566234, 40.49944),
          new TrimbleMaps.LngLat(-74.629749, 40.26118)
        ],
        reportType: [
          TrimbleMaps.Common.ReportType.MILEAGE,
          TrimbleMaps.Common.ReportType.DETAIL
        ]
      });
    
    
      // *** THIS IS THE LISTENER ****
      myRoute.on("report", function (reports) {
        console.log(reports);
      });
    

    Reference: https://developer.trimblemaps.com/maps-sdk/guide/routing/