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:
- If a vehicle moves out of a specified area(geofence) then we can send an alert to the owner of the vehicle.
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[php] 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 }); [/php] |
Here we are drawing a rectangle shaped geofence using some properties like fillColor,opacity etc.
It looks something like this:
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:
1 2 3 4 5 6 7 8 9 10 11 |
[php] 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 } [/php] |
That was all folks!
how to make geofence that user can change or can setup by himself?