Click here to Skip to main content
13,803,738 members
Click here to Skip to main content
Add your own
alternative version


53 bookmarked
Posted 5 Mar 2010
Licenced CPOL

A Simple Geo Fencing Using Polygon Method

, 7 Mar 2010
Rate this:
Please Sign up or sign in to vote.
A Simple Geo fencing using polygon method


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:


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


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 India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionRegarding XML Point Pin
Member 1349301115-Dec-17 1:53
memberMember 1349301115-Dec-17 1:53 
QuestionNegative longitudes Pin
Member 1312579413-Apr-17 7:01
memberMember 1312579413-Apr-17 7:01 
QuestionHow can we add a 3rd Dimension ? Pin
Ran Shalit22-May-16 11:47
memberRan Shalit22-May-16 11:47 
QuestionReturn Always Pin
Fatih Sahinbas18-Apr-16 2:06
memberFatih Sahinbas18-Apr-16 2:06 
QuestionCheck Latlong is inside rectangle geofence or not Pin
Chintan Desai198823-Dec-14 23:11
memberChintan Desai198823-Dec-14 23:11 
AnswerRe: Check Latlong is inside rectangle geofence or not Pin
Member 121856984-Dec-15 0:40
memberMember 121856984-Dec-15 0:40 
QuestionAbout XML Pin
chetan.kamurti15-Sep-14 2:17
memberchetan.kamurti15-Sep-14 2:17 
QuestionReturn always => False Pin
Member 983553714-Feb-13 8:22
memberMember 983553714-Feb-13 8:22 
Questionhow mark road and get coordinates Pin
HarshaAluthge22-May-12 20:26
memberHarshaAluthge22-May-12 20:26 
QuestionAccessing coordinates of polygon Pin
Member 82674567-May-12 5:46
memberMember 82674567-May-12 5:46 
GeneralMy vote of 3 Pin
kiran kumar 41221-Dec-11 22:33
memberkiran kumar 41221-Dec-11 22:33 
QuestionQuick question Pin
Member 803356218-Nov-11 7:09
memberMember 803356218-Nov-11 7:09 
Questionneed help! Pin
Reinhard Navarro5-Oct-11 19:26
memberReinhard Navarro5-Oct-11 19:26 
QuestionFormula Error Pin
borno5-Aug-11 9:49
memberborno5-Aug-11 9:49 
QuestionGeofencing? Polygon? Pin
bulleh4-Jul-11 1:40
memberbulleh4-Jul-11 1:40 
AnswerRe: Geofencing? Polygon? Pin
RajuBhupathi5-Jul-11 0:41
memberRajuBhupathi5-Jul-11 0:41 
GeneralRe: Geofencing? Polygon? Pin
bulleh7-Jul-11 2:22
memberbulleh7-Jul-11 2:22 
GeneralRe: Geofencing? Polygon? Pin
bulleh20-Jul-11 1:22
memberbulleh20-Jul-11 1:22 
GeneralRe: Geofencing? Polygon? Pin
borno5-Aug-11 9:40
memberborno5-Aug-11 9:40 
GeneralRe: Geofencing? Polygon? Pin
bulleh6-Aug-11 0:38
memberbulleh6-Aug-11 0:38 
GeneralRe: Geofencing? Polygon? Pin
zinobu24-Aug-11 8:30
memberzinobu24-Aug-11 8:30 
GeneralRe: Geofencing? Polygon? Pin
Sivaprasad SR10-May-17 3:35
memberSivaprasad SR10-May-17 3:35 
GeneralGetting decimal data to work. Pin
domanet16-Jun-11 1:23
memberdomanet16-Jun-11 1:23 
GeneralRe: Getting decimal data to work. Pin
RajuBhupathi19-Jun-11 23:44
memberRajuBhupathi19-Jun-11 23:44 
GeneralRe: Getting decimal data to work. Pin
domanet20-Jun-11 8:04
memberdomanet20-Jun-11 8:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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

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