Geofencing is actually a process of creating/defining geofence on the maps. It can be either circle,rectangle or n-sided polygon shaped. Geofence is nothing but a virtual boundary that is drawn on map. Geofence have many applications like:

 

  1. If a vehicle moves out of a specified area(geofence) then we can send an alert to the owner of the vehicle.
  2. If a prisoner tied with a gps device tries to leave the premises then security people can be alerted.

 

How it works ?

 

The gps device regularly sends the lat-long coordinates which we can examine to see if it falls inside/outside the geofence boundary and then take appropriate action accordingly.

 

How to create geofence ? 

 

Google Maps API provides geofence objects:


var geoFence = new google.maps.Rectangle({
strokeColor: 'rgba(0, 147, 74, 0.8)',
strokeOpacity: 0.8,
editable: true,
strokeWeight: 2,
fillColor: 'rgba(96, 198, 147, 0.6)',
fillOpacity: 0.6,
map: map,
draggable: true,

bounds:calcBounds(new google.maps.LatLng(latitude,
longitude), new google.maps.Size(75,75)),
info: contentString
});

Here we are drawing a rectangle shaped geofence using some properties like fillColor,opacity etc.

 

It looks something like this:

 

geofence

 

A rectangle geofence boundary is determined by north-east and south-west coordinates.  So if we want to check an incoming gps coordinates against the geofence we could do so using the following pseudo-code:


if(gps_longitude>=geofence.north && gps_longitude<=geofence.south) && (gps_latitude>=geofence.west && gps_latitude<=geofence.east) ){

// Coordinate lies within the geofence i.e. unauthorized area

// Send notification

}

That was all folks!