-2

I have a table called warehouses, which has latitude & longitude columns. I am searching nearby warehouses from my current location (Providing latitude & longitude, radius) to find the warehouses nearby me in a given radius.

this operation needs to be performed by the query, not manual functions.

  • You should check this question https://stackoverflow.com/questions/2234204/find-nearest-latitude-longitude-with-an-sql-query – Jubaz Dec 28 '21 at 11:03

2 Answers2

1

For those who might need it, I have solved this problem.

$lati = (float)$request->latitude;
$longt = (float)$request->longtitude;
$rad = (int)$request->radius;



$distance_result = "(6371 * acos(cos(radians($lati))
* cos(radians(latit))
* cos(radians(longt)
- radians($longt))
+ sin(radians($lati))
* sin(radians(latit))))";


$warhouses = Warhouse::select('id', 'name')
    ->selectRaw("{$distance_result} AS distance")
    ->whereRaw("{$distance_result} < ?", [$rad])
    ->get();



return view('test')->with('nearbys', $warhouses);
  • can you elaborate on this query? I am doing similar work, but I don't know what is 6371 in there. and also how these formulas are actually working, – Amir Arain Nov 15 '22 at 05:31
0

Use This mysql Function : ST_Distance_Sphere

Supported since MySql 5.7 version and above.

select ST_Distance_Sphere(point(lon,lat), point(lon,lat))

John
  • 578
  • 1
  • 3
  • 15