# A Simple Geo Fencing Using Polygon Method

, 7 Mar 2010 CPOL
 Rate this:
Please Sign up or sign in to vote.
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();
ds.ReadXml("PolygonPoints.XML");

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;
points.Add(p);
}
} ```

## 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
lblResult.Text = "Point not found in the route";```

## License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

## About the Author

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

## Comments and Discussions

 First Prev Next
 Check Latlong is inside rectangle geofence or not Chintan Desai1988 23-Dec-14 23:11
 About XML chetan.kamurti 15-Sep-14 2:17
 Return always => False Member 9835537 14-Feb-13 8:22
 how mark road and get coordinates HarshaAluthge 22-May-12 20:26
 Accessing coordinates of polygon Member 8267456 7-May-12 5:46
 My vote of 3 kiran kumar 412 21-Dec-11 22:33
 Quick question Member 8033562 18-Nov-11 7:09
 need help! Reinhard Navarro 5-Oct-11 19:26
 Formula Error borno 5-Aug-11 9:49
 Geofencing? Polygon? bulleh 4-Jul-11 1:40
 Re: Geofencing? Polygon? RajuBhupathi 5-Jul-11 0:41
 Re: Geofencing? Polygon? bulleh 7-Jul-11 2:22
 Re: Geofencing? Polygon? bulleh 20-Jul-11 1:22
 Re: Geofencing? Polygon? borno 5-Aug-11 9:40
 Re: Geofencing? Polygon? bulleh 6-Aug-11 0:38
 Re: Geofencing? Polygon? zinobu 24-Aug-11 8:30
 Getting decimal data to work. domanet 16-Jun-11 1:23
 Re: Getting decimal data to work. RajuBhupathi 19-Jun-11 23:44
 Re: Getting decimal data to work. domanet 20-Jun-11 8:04
 How to convert coordinates to XML file? Tadysas 26-Mar-11 7:12
 Re: How to convert coordinates to XML file? Tadysas 26-Mar-11 7:35
 formula error Noman Aftab 29-Dec-10 2:48
 ask about coordinat in the xml file? kanvista 9-Jul-10 0:17
 Re: ask about coordinat in the xml file? oldsap 13-Jul-10 17:28
 Re: ask about coordinat in the xml file? RajuBhupathi 14-Jul-10 0:22
 Re: ask about coordinat in the xml file? oldsap 3-Aug-10 23:03
 Re: ask about coordinat in the xml file? RajuBhupathi 4-Aug-10 21:01
 Re: ask about coordinat in the xml file? oldsap 13-Aug-10 15:39
 Re: ask about coordinat in the xml file? RajuBhupathi 16-Aug-10 2:09
 Re: ask about coordinat in the xml file? RajuBhupathi 16-Aug-10 2:10
 Re: ask about coordinat in the xml file? oldsap 16-Aug-10 14:23
 Re: ask about coordinat in the xml file? oldsap 17-Aug-10 21:48
 Re: ask about coordinat in the xml file? RajuBhupathi 17-Aug-10 22:56
 Re: ask about coordinat in the xml file? oldsap 17-Aug-10 23:07
 Re: ask about coordinat in the xml file? RajuBhupathi 17-Aug-10 23:22
 Re: ask about coordinat in the xml file? oldsap 17-Aug-10 23:23
 Re: ask about coordinat in the xml file? oldsap 17-Aug-10 23:59
 Re: ask about coordinat in the xml file? RajuBhupathi 18-Aug-10 0:01
 How do you capture the points of the polygon? Qwertie 8-Mar-10 19:15
 Re: How do you capture the points of the polygon? RajuBhupathi 9-Mar-10 20:52
 ZIP corrupt Member 3279273 7-Mar-10 4:17
 Re: ZIP corrupt RajuBhupathi 7-Mar-10 5:34
 Re: ZIP corrupt Member 3279273 7-Mar-10 8:41
 Ok, ZIP 100% Thanks. Att, Valdir Brazil.
 Re: My vote of 2 RajuBhupathi 7-Mar-10 5:33
 Last Visit: 31-Dec-99 19:00     Last Update: 1-Apr-15 10:08 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150331.1 | Last Updated 7 Mar 2010
Article Copyright 2010 by RajuBhupathi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid