ads

Latitude Longitude Radius Query Laravel

How to calculate distance between two latitude and longitude in Laravel

In this tutorial, I am trying to share with you about Latitude Longitude Radius Query Laravel. On map task location you can use this code which I'll write here below with details.

So many task-related maps into laravel like display all location dynamically into your laravel project or if you select any location by any dropdown from the location like Lisbon, Portugal and then after selection, it will display the nearest place which is nearby this selected location name is Lisbon, Portugal


My friend when you find the nearby places you have one database of the project also there you need to store latitude and longitude. Suppose here I am taking an example of spots places that are nearest by Lisbon selected location so every spot must have the latitude and longitude into your database.

So let's start the task how to apply nearest How to find nearest location using latitude and longitude in laravel

Radius you can take dynamically whatever it is you can find the data if you have every spot of latitude & longitude.


Okay so Query I am writing below


  $sqlQuery = "select * from spots where lat!='' and lng!='' and ( 3959 * acos( cos( radians(".$latitude.") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$longitude.") ) + sin( radians(".$latitude.") ) * sin( radians( lat ) ) ) ) < ".$radius_data->value."";
          
OR

  $laravelQuery = DB::table("spots")
  ->select("*"
  ,DB::raw( 3959 * acos( cos( radians(".$latitude.") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$longitude.") ) + sin( radians(".$latitude.") ) * sin( radians( lat ) ) ) ) )
  ->whereNotNull('spots.lat')
  ->whereNotNull('spots.lng')
  ->groupBy("spots.id")
  ->get();
Here is the one of the best solution for MySQL select closest lat/long to specific location laravel

Happy coding :)

Post a Comment

Previous Post Next Post