12,294,635 members (69,159 online)
alternative version

103.9K views
47 bookmarked
Posted

# A Simple Geo Fencing Using Polygon Method

, 7 Mar 2010 CPOL
 Rate this:
A Simple Geo fencing using polygon method

## Introduction

One of the important feature of GPS Tracking software using GPS Tracking devices is geo-fencing and its ability to help keep track of assets. Geo-fencing allows users of a GPS Tracking Solution to draw zones (i.e., a Geo Fence) around places of work, customer’s sites and secure areas. These geo fences when crossed by a GPS equipped vehicle or person can trigger a warning to the user or operator via SMS or Email.

## Geo Fence

A Geo fence is a virtual perimeter on a geographic area using a location-based service, so that when the geo fencing device enters or exits the area, a notification is generated. The notification can contain information about the location of the device and might be sent to a mobile telephone or an email account. Reference: http://en.wikipedia.org/wiki/Geofence.

## Background

For geo-fencing, I used Polygonal geo-fencing method where a polygon is drawn around the route or area. Using this method, GPS Tracking devices can be tracked either inside or outside of the polygon.

## Determining a Point

The function will return `true `if the point X,Y is inside the polygon, or `false `if it is not. If the point is exactly on the edge of the polygon, then the function may return `true `or `false`. Thanks for the article “Determining Whether A Point Is Inside A Complex Polygon”.

```public bool FindPoint(double X, double Y)
{
int sides = this.Count() - 1;
int j = sides - 1;
bool pointStatus = false;
for (int i = 0; i < sides; i++)
{
if (myPts[i].Y < Y && myPts[j].Y >= Y ||
myPts[j].Y < Y && myPts[i].Y >= Y)
{
if (myPts[i].X + (Y - myPts[i].Y) /
(myPts[j].Y - myPts[i].Y) * (myPts[j].X - myPts[i].X) < X)
{
pointStatus = !pointStatus ;
}
}
j = i;
}
return pointStatus;
}```

## Creating a Polygon

On the map, draw a polygon to the area which is to be geo-fenced and capture the corner points of the polygon and store into XML file (see: PolygonPoints.XML). `loadData() `function will create a polygon using defined corner points in the XML file.

```private void loadData()
{
DataSet ds = new DataSet();

foreach (DataRow dr in ds.Tables[0].Rows)
{
Point p = new Point();

//Convert Latitude into degrees
String Lat = dr[0].ToString();
double LatSec = Double.Parse(Lat.Substring(4, 4)) / 6000;
double LatMin = (Double.Parse(Lat.Substring(2, 2)) + LatSec) / 60;
p.X = Double.Parse(Lat.Substring(0, 2)) + LatMin;

//Convert Longitude into degrees
String Long = dr[1].ToString();
double LongSec = Double.Parse(Long.Substring(5, 4)) / 6000;
double LongMin = (Double.Parse(Long.Substring(3, 2)) + LongSec) / 60;
p.Y = Double.Parse(Long.Substring(0, 3)) + LongMin;
}
} ```

## Sample Code

When you run and enter latitude and longitude outside the polygon, then a message shows point not found in the route and otherwise it shows point found in the route.

```PolyGon myRoute = new PolyGon(points);
bool stat = myRoute.FindPoint(Double.Parse(txtLat.Text.ToString()),
Double.Parse(txtLang.Text.ToString()));
if(stat)
{
lblResult.Text = "Point found in the route";
}
else

## Share

 Architect Vajra Infratech Pvt. Ltd., India
No Biography provided

## You may also be interested in...

 First Prev Next
 How can we add a 3rd Dimension ? Ran Shalit22-May-16 10:47 Ran Shalit 22-May-16 10:47
 Return Always Fatih Sahinbas18-Apr-16 1:06 Fatih Sahinbas 18-Apr-16 1:06
 Check Latlong is inside rectangle geofence or not Chintan Desai198823-Dec-14 22:11 Chintan Desai1988 23-Dec-14 22:11
 Re: Check Latlong is inside rectangle geofence or not Member 121856983-Dec-15 23:40 Member 12185698 3-Dec-15 23:40
 About XML chetan.kamurti15-Sep-14 1:17 chetan.kamurti 15-Sep-14 1:17
 Return always => False Member 983553714-Feb-13 7:22 Member 9835537 14-Feb-13 7:22
 how mark road and get coordinates HarshaAluthge22-May-12 19:26 HarshaAluthge 22-May-12 19:26
 Accessing coordinates of polygon Member 82674567-May-12 4:46 Member 8267456 7-May-12 4:46
 My vote of 3 kiran kumar 41221-Dec-11 21:33 kiran kumar 412 21-Dec-11 21:33
 Quick question Member 803356218-Nov-11 6:09 Member 8033562 18-Nov-11 6:09
 need help! Reinhard Navarro5-Oct-11 18:26 Reinhard Navarro 5-Oct-11 18:26
 Formula Error borno5-Aug-11 8:49 borno 5-Aug-11 8:49
 Geofencing? Polygon? bulleh4-Jul-11 0:40 bulleh 4-Jul-11 0:40
 Re: Geofencing? Polygon? RajuBhupathi4-Jul-11 23:41 RajuBhupathi 4-Jul-11 23:41
 Re: Geofencing? Polygon? bulleh7-Jul-11 1:22 bulleh 7-Jul-11 1:22
 Re: Geofencing? Polygon? bulleh20-Jul-11 0:22 bulleh 20-Jul-11 0:22
 Re: Geofencing? Polygon? borno5-Aug-11 8:40 borno 5-Aug-11 8:40
 Re: Geofencing? Polygon? bulleh5-Aug-11 23:38 bulleh 5-Aug-11 23:38
 Re: Geofencing? Polygon? zinobu24-Aug-11 7:30 zinobu 24-Aug-11 7:30
 Getting decimal data to work. domanet16-Jun-11 0:23 domanet 16-Jun-11 0:23
 Re: Getting decimal data to work. RajuBhupathi19-Jun-11 22:44 RajuBhupathi 19-Jun-11 22:44
 Re: Getting decimal data to work. domanet20-Jun-11 7:04 domanet 20-Jun-11 7:04
 How to convert coordinates to XML file? Tadysas26-Mar-11 6:12 Tadysas 26-Mar-11 6:12
 Re: How to convert coordinates to XML file? Tadysas26-Mar-11 6:35 Tadysas 26-Mar-11 6:35
 formula error Noman Aftab29-Dec-10 1:48 Noman Aftab 29-Dec-10 1:48
 ask about coordinat in the xml file? kanvista8-Jul-10 23:17 kanvista 8-Jul-10 23:17
 Re: ask about coordinat in the xml file? oldsap13-Jul-10 16:28 oldsap 13-Jul-10 16:28
 Re: ask about coordinat in the xml file? RajuBhupathi13-Jul-10 23:22 RajuBhupathi 13-Jul-10 23:22
 Re: ask about coordinat in the xml file? oldsap3-Aug-10 22:03 oldsap 3-Aug-10 22:03
 Re: ask about coordinat in the xml file? RajuBhupathi4-Aug-10 20:01 RajuBhupathi 4-Aug-10 20:01
 Re: ask about coordinat in the xml file? oldsap13-Aug-10 14:39 oldsap 13-Aug-10 14:39
 Re: ask about coordinat in the xml file? RajuBhupathi16-Aug-10 1:09 RajuBhupathi 16-Aug-10 1:09
 Re: ask about coordinat in the xml file? RajuBhupathi16-Aug-10 1:10 RajuBhupathi 16-Aug-10 1:10
 Re: ask about coordinat in the xml file? oldsap16-Aug-10 13:23 oldsap 16-Aug-10 13:23
 Re: ask about coordinat in the xml file? oldsap17-Aug-10 20:48 oldsap 17-Aug-10 20:48
 Re: ask about coordinat in the xml file? RajuBhupathi17-Aug-10 21:56 RajuBhupathi 17-Aug-10 21:56
 Re: ask about coordinat in the xml file? oldsap17-Aug-10 22:07 oldsap 17-Aug-10 22:07
 Re: ask about coordinat in the xml file? RajuBhupathi17-Aug-10 22:22 RajuBhupathi 17-Aug-10 22:22
 Re: ask about coordinat in the xml file? oldsap17-Aug-10 22:23 oldsap 17-Aug-10 22:23
 Re: ask about coordinat in the xml file? oldsap17-Aug-10 22:59 oldsap 17-Aug-10 22:59
 Re: ask about coordinat in the xml file? RajuBhupathi17-Aug-10 23:01 RajuBhupathi 17-Aug-10 23:01
 How do you capture the points of the polygon? Qwertie8-Mar-10 18:15 Qwertie 8-Mar-10 18:15
 Re: How do you capture the points of the polygon? RajuBhupathi9-Mar-10 19:52 RajuBhupathi 9-Mar-10 19:52
 ZIP corrupt Member 32792737-Mar-10 3:17 Member 3279273 7-Mar-10 3:17
 Re: ZIP corrupt RajuBhupathi7-Mar-10 4:34 RajuBhupathi 7-Mar-10 4:34
 Re: ZIP corrupt Member 32792737-Mar-10 7:41 Member 3279273 7-Mar-10 7:41
 Re: My vote of 2 RajuBhupathi7-Mar-10 4:33 RajuBhupathi 7-Mar-10 4:33
 Last Visit: 31-Dec-99 18:00     Last Update: 25-May-16 21:13 Refresh 1