[英]Distance between two locations in Laravel using latitude and longitude in database query
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.此操作需要通过查询来执行,而不是手动功能。
Use This mysql Function: ST_Distance_Sphere使用此 mysql Function: ST_Distance_Sphere
Supported since MySql 5.7 version and above.从 MySql 5.7 及以上版本开始支持。
select ST_Distance_Sphere(point(lon,lat), point(lon,lat))
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.