What is Geofencing and how we will use it in our application

Amer Alhelali
3 min readMar 25, 2024

--

Geofencing is creating virtual boundaries around specific geographic areas. If the user is inside this area, you can take action such as sending a notification sending a text message, or allowing the user to use the system.

How does it work theoretically?

you need two things, first the longitude, latitude, and radius of the location and we will call it fencing second thing is the longitude and the latitude of the user.
after we get the data that we want, now we will calculate the distance the user’s location and the fencing location after that we will compare between the distance and the radius if the radius is more than the distance it means the user is inside the fencing.

How do We Implement this?

in this example, we will use Laravel so let’s assume you have an endpoint and don’t want anyone outside the KAFD(King Abdullah Financial District) using this endpoint. So we will create middleware and this middleware will check if you are allowed to use this endpoint or not. after that, we will create a trite to handle everything related to Geofencing like calculating the radius and changing the radius unit.

First of all, we need to get the latitude, and radius of the KAFD and we will put it on the config.

ALLOWED_LOCATION_LONGITUDE="46.640885"
ALLOWED_LOCATION_LATITUDE="24.764790"
ALLOWED_LOCATION_RADIUS="633.82"

after we set the fencing data let us create the trite :

trait HasGeoFencing
{

public function checkGeoFencing(float $longitude, float $latitude): bool
{
try {
$distance = $this->calculateDistance($longitude, $latitude);

if ($distance <= config("app.allowed_location.radius")) {
return true;
}
return false;
} catch (Exception $exception) {
report($exception);
return false;
}
}

private function calculateDistance(float $givenLongitude, float $givenLatitude)
{
$theta = $givenLongitude - config("app.allowed_location.longitude");
$dist = sin(deg2rad(config("app.allowed_location.latitude"))) * sin(deg2rad($givenLatitude)) + cos(deg2rad(config("app.allowed_location.latitude"))) * cos(deg2rad($givenLatitude)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);

$miles = $dist * 60 * 1.1515;
$km = $miles * 1.609344;
return $km * 1000;
}
}

after we create the trait let us create the middleware

class CheckIsLocationAllowed
{
use HasGeoFencing;
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{

if (!$longitude=$request->get("longitude")) {
return response()->json(["message" => "No longitude provided"], 400);
}
if (!$latitude=$request->get("latitude")) {
return response()->json(["message" => "No latitude provided"], 400);
}

if (!$this->checkGeoFencing(longitude: $longitude, latitude: $latitude)) {
return response()->json(["message" => "Unauthorized"], 401);
}
return $next($request);
}
}

now it is time to test, we will send two requests to get the users one inside the fencing and one outside.

so let us start with the inside fencing

The purple circle represents the user’s current location and the position is: 24.767720,46.642261

this is the result and you can see that the API allowed the user to access this endpoint.

now let's send another request but now outside the fencing

The purple circle represents the user’s current location and the position is: 24.768734,46.647067

this is the result and you can see that the user is not Unauthorized to use this endpoint.

finally, this one from many cases that you can use and handle the Geofencing Thanks for reading🤝.

--

--