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

SCRIPT: Inside geofence named in custom field

Sat Sep 05, 2015 2:25 pm

SCRIPT: Inside geofence named in custom field

This guide shows how you can write a geofence rule that checks if a vehcile is inside a geofence named by a custom field. In this way you can write one single event rule, which checks for different geofences depending on vehicle.

Test Setup

Create a Custom Field named "Fence".
How to: http://gpsgate.com/blogComment/custom_fields

Create a Geofence Tag named "Pipelines".
Create a Geofence called e.g. "Pipeline33". Add Pipeline33 to Geofence Tag Pipelines

Edit you test vehicle and set the custom filed "Fence" to "Pipeline33"

Create Event Rule

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

Code: Select all

// Find geofences at current position.
var fences = geofence.find(trackPoint.position.longitude, trackPoint.position.latitude, 'Pipelines');
var isInside = false;

context.setVariable('PipeName', '-');

if(fences.length > 0)
{
  // Get name of geofence from Custom Field
  var pipeName = session.user.getCustomFieldValue('Fence');

  if(pipeName != null)
  {
	// Check if named geofence is as current position.
	
	for(var iIndex = 0; iIndex < fences.length; iIndex++)
	{
	  if(fences[iIndex].name == pipeName)
	  {
		isInside = true;
		// Use [PipeName] as variable in Notifiers
		context.setVariable('PipeName', pipeName);
		break;
	  }
	}
  }
}

// Return true if inside named geofence.
return isInside;
Notifiers

Use the function context.setVariable() to create variables you can use in the Notifiers. For example a "Report Argument" if you want to create a column in the EV1000 report. Or an "Argument" notifier if you want to create on column in the Event window in the Workspace.

In the example above, the named geofence will be in variable [PipeName]

Related topics

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

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

Allow users to select Event Rules using Custom Fields with scripting
http://gpsgate.com/blogComment/allow_users_to_select_event_rules_using_custom_fields_with_scripting

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

Regards,
Johan

GpsGate Support

niwewenamimi
Posts: 3
Joined: Wed Feb 07, 2018 6:29 am

Re: SCRIPT: Inside geofence named in custom field

Wed Feb 06, 2019 3:28 pm

By saying Geofene Tag . Do you mean Geofence group , because there is no Tags in Geofence module

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

Re: SCRIPT: Inside geofence named in custom field

Wed Feb 06, 2019 5:05 pm

Yes.
Regards,
Johan

GpsGate Support

niwewenamimi
Posts: 3
Joined: Wed Feb 07, 2018 6:29 am

Re: SCRIPT: Inside geofence named in custom field

Fri Feb 08, 2019 7:51 am

i have written the code below but still produce an empty array even if the vehicle is in the geofence indicate ., See picture

Image

Return to “GpsGate for Developers”