User avatar
johan
GpsGate
Posts: 15225
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

SCRIPT: Distance between POIs (customer sites)

Sat Jan 09, 2016 11:30 am

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.

Related topics

Tips when developing Script Expressions.
http://forum.gpsgate.com/topic.asp?TOPIC_ID=18303

More info on Script Expressions here:
http://gpsgate.com/blogComment/script_expressions

Displaying arguments on Event Rule reports
http://gpsgate.com/blogComment/displaying_arguments_on_event_rule_reports

Regards,
Johan

GpsGate Support

rajiv
Posts: 120
Joined: Thu Nov 27, 2014 2:27 pm

RE: SCRIPT: Distance between POIs (customer sites)

Fri Mar 04, 2016 2:24 pm

What if the customer POIs are under multiple catagories ?

Return to “GpsGate for Developers”