First of all while registering the futsal my system ask for location and saves latitude and longitude of that futsal in database. And when user visits my system, it ask for user's location and saves latitude and longitude in session.
with the help of these futsal and user's latitude and location i want to calculate the distance between them in KM and show in the landing page.
I have tried
$lat = $request->session()->get("latitude");
$long = $request->session()->get("longitude");
$data['futsals'] = DB::table("users")
->where('is_futsal',1)
->select("*",
DB::raw("6371 * acos(cos(radians(".$lat."))
* cos(radians(users.latitude))
* cos(radians(users.longitude) - radians(".$long."))
+ sin(radians(".$lat."))
* sin(radians(users.latitude))) AS distance"))
->get();
It gives me distance but when ever i want to show the distance between user's and futsal the distance varies. I registered the futsal and login as a user from same place. which means the distance between them should be 0 but sometimes it gives me 0 also it gives me 1.9 km or 3.5 KM