algorithm to define a geofence and see if a point is inside/outside it
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A geofence is just a geometric boundary around an area of interest, plus a rule for deciding whether a location lies inside or outside that boundary. The right algorithm depends on the fence shape: circles are handled with distance checks, while arbitrary polygons usually use a point-in-polygon test such as ray casting.
Start by Choosing the Geofence Shape
Most practical geofences use one of two models:
- a circle defined by center and radius
- a polygon defined by ordered vertices
A circular geofence is simpler and faster. A polygonal geofence is more flexible when the real-world shape does not fit a radius around one point.
Circular Geofence Algorithm
For a circle, you store:
- center latitude
- center longitude
- radius
Then you compute the distance from the point to the center. If that distance is less than or equal to the radius, the point is inside.
For geographic coordinates, the Haversine formula is a common choice:
This is usually accurate enough for ordinary mobile and mapping applications.
Polygon Geofence Algorithm
A polygon geofence is defined by a list of corner points. The standard inside-outside test is ray casting.
The idea is:
- draw an imaginary horizontal ray from the point
- count how many polygon edges the ray crosses
- odd count means inside, even count means outside
Here is a simple implementation on planar coordinates:
For small local regions, treating latitude and longitude as planar coordinates is often acceptable. For larger regions or high-accuracy GIS work, you need to account for projection and Earth curvature more carefully.
Boundary Rules Must Be Defined Explicitly
You also need a business rule for points that lie exactly on the boundary. Some systems count boundary points as inside. Others treat them as a separate case.
That sounds minor, but it matters for alerts and repeated entry-exit events. If you do not define the edge rule clearly, users can get inconsistent behavior near the fence border.
Practical Geofencing Considerations
Real systems usually add more than pure geometry:
- GPS noise filtering
- hysteresis so the state does not flicker on the border
- timestamp checks
- minimum dwell time before firing an event
For example, if a device oscillates around the boundary by a few meters, a strict inside-outside calculation may trigger repeated enter and exit events. Many systems fix that by using two radii or by requiring the point to remain inside for a minimum time.
Which Algorithm Should You Use?
Use a circle when:
- one center point defines the area well
- speed is more important than shape fidelity
- the fence is small and simple
Use a polygon when:
- the boundary follows streets, parcels, or campus edges
- a radius would include too much irrelevant area
- shape accuracy matters more than the absolute cheapest computation
Common Pitfalls
- Using straight Euclidean distance on raw latitude-longitude values for large regions can produce misleading results.
- Forgetting to define what happens on the exact boundary creates inconsistent behavior.
- Applying a planar polygon test to very large geographic areas without projection awareness can reduce accuracy.
- Ignoring GPS noise often causes repeated enter-exit flicker near the fence edge.
- Choosing a circle just because it is simple can be a bad product decision if the real area is highly irregular.
Summary
- A geofence is usually either a circle or a polygon.
- Circular geofences use a distance test, often with the Haversine formula.
- Polygon geofences usually use a point-in-polygon test such as ray casting.
- Boundary handling and GPS noise matter as much as the raw geometry.
- Pick the simplest fence shape that still models the real area accurately enough for the application.

