12,630,386 members (32,394 online)
alternative version

114.7K 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...

 Pro Pro

 View All Threads First Prev Next
 Geofencing? Polygon? bulleh4-Jul-11 1:40 bulleh 4-Jul-11 1:40
 Re: Geofencing? Polygon? RajuBhupathi5-Jul-11 0:41 RajuBhupathi 5-Jul-11 0:41
 Re: Geofencing? Polygon? bulleh7-Jul-11 2:22 bulleh 7-Jul-11 2:22
 Re: Geofencing? Polygon? bulleh20-Jul-11 1:22 bulleh 20-Jul-11 1:22
 Re: Geofencing? Polygon? borno5-Aug-11 9:40 borno 5-Aug-11 9:40
 Re: Geofencing? Polygon? bulleh6-Aug-11 0:38 bulleh 6-Aug-11 0:38
 Re: Geofencing? Polygon? zinobu24-Aug-11 8:30 zinobu 24-Aug-11 8:30
 Last Visit: 31-Dec-99 19:00     Last Update: 7-Dec-16 18:05 Refresh 1