By geofence I'm assuming polygon, so you would be looking for a point in polygon algorithm. This can be done fairly quickly, even in JavaScript code.
Turf.js is a fairly popular JavaScript library that does spatial calculations. With that you would most likely want to use the booleanPointInPolygon
function: https://turfjs.org/docs/api/booleanPointInPolygon
That said, the actual code for point in polygon test is fairly small and you could simply do the following without a library:
function pointInPolygon(point, polygonRing) {
let x = point[0], y = point[1];
let inside = false;
for (let i = 0, j = polygonRing.length - 1; i < polygonRing.length; j = i++) {
let xi = polygonRing[i][0], yi = polygonRing[i][1];
let xj = polygonRing[j][0], yj = polygonRing[j][1];
let intersect = ((yi > y) !== (yj > y)) &&
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
// Example usage:
const polygon = [[1,1], [1,3], [3,3], [3,1]];
const point = [2,2];
console.log(pointInPolygon(point, polygon)); // true
The above code tests if a GeoJSON Position is within a polygon ring. This can be expanded out to support polygons with holes, and multipolygons if needed as follows:
//Takes in a GeoJSON Position and the coordinates of a Polygon object (number[][][]).
function pointInPolygonWithHoles(point, polygonCoords) {
if(!polygonCoords || polygonCoords.length === 0) {
return false;
}
//The first ring of a polygon should be the exterior ring and all other rings are holes.
//So test that the point is in the first ring and not in the others.
let isWithin = pointInPolygon(point, polygonCoords[0]);
if(isWithin && polygonCoords.length > 1) {
//Check the holes.
for(let i = 1; i < polygonCoords.length; i++){
if(pointInPolygon(point, polygonCoords[i])){
//If we make it here, the point is in the hole and thus, not in the polyogn. We can fail quickly now and don't need to test the other polygons.
isWithin = false;
break;
}
}
}
return isWithin;
}
//Takes in a GeoJSON Position and the coordinates of a MultiPolygon object (number[][][][]).
function pointInMultiPolygon(point, multiPolygonCoords) {
if(!multiPolygonCoords || multiPolygonCoords.length === 0) {
return false;
}
//If the point is in any polygon, then it's successful.
for(let i = 1; i < multiPolygonCoords.length; i++){
if(pointInPolygonWithHoles(point, multiPolygonCoords[i])){
return true;
}
}
return false;
}
//Takes in a GeoJSON Position and any GeoJson Geometry object.
function pointInGeometry(point, geometry){
if(geometry.type === 'Polygon'){
return pointInPolygonWithHoles(geometry.coordinates);
} else if(geometry.type === 'MultiPolygon'){
return pointInMultiPolygon(geometry.coordinates);
}
//Lines and Point types should fail.
return false;
}
If you are using a different programming language, there are many spatial math libraries available depending on your target language. For example, if using .NET, the NET topology suite is popular: https://github.com/NetTopologySuite/NetTopologySuite