Script distance between POIs (customer sites)

Here is a script that calculates distance between customer sites (POIs) a vehicle is visiting.

We use the report.context.distance object to do this.

report.context.distance;
Accumulated distance within a day. Can be modified using setDistance(value).

report.context.setDistance(value);
Change value of daily distance.

Inside scripts SI units are always used. In this case meters. The unit is set in the report layout under SiteAdmin -> Reporting -> Manage Reports.

To know that we leave a customer site, we need to remeber if we were are at a customer site last message or not. To do this we use the context object.

contect.state;
Stores up to 255 bytes of data between separate evaluations.

context.setState(value);
Saves value or object to state.

Change 'My POI Category' to the name of the POI category where you have your customer sites.

Test Setup

Tips when developing Script Expressions.
https://support.gpsgate.com/hc/en-us/articles/360026121574

The Event Rule must be disabled (Enabled set to off) in Step 1. of the Event Rule Wizard for the "report.*" scripting API to work.

Create Event Rule

Create an Event Rule with a Script Expression like the one below:

Code: Select all

// START OF SCRIPT

// Replace 'My POI Category' with the name of the POI category where you have your customer sites.
var pois = poi.find(trackPoint.position.longitude, trackPoint.position.latitude, 500, 'My POI Category')
var arrived = false;

if(pois.length > 0)
{
  context.setVariable('ClientName', pois[0].name);
  // Arrived at customer. Set distance.
  context.setVariable('Distance', report.context.distance);
  arrived = true;
}
else
{
  context.setVariable('ClientName', '-');
}

// Reset distance if we just left customer site.

var last = context.state;
if(last != null && last.arrived == true && arrived == false)
  // We just left the customer, set distance to zero
  report.context.setDistance(0);

// Remeber if we were at customer for next messgae.
context.setState({arrived:arrived});
  
return arrived;

// END OF SCRIPT

Notifiers

The distance is saved to a Notifier variable named [Distance]
Please add one more Reporting Argument with the title Distance and value [Distance].

Inside scripts SI units are always used. In this case meters. The unit is set in the report layout under SiteAdmin -> Reporting -> Manage Reports.

Use the EV1000 report to show the report.