简体   繁体   English

Laravel中两个位置之间的距离在数据库查询中使用经纬度

[英]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.

相关问题 用于查找使用纬度和经度之间的距离的Sql返回错误的距离 - Sql for finding the distance between using latitude and longitude returns wrong distance 使用经纬度邮政编码数据库选择3个最接近的位置 - Selecting 3 closest locations using latitude-longitude-postcode database 如何使用php使用经度和纬度获取点之间的距离? - how to get distance between points using latitude and longitude using php? Laravel 按经纬度距离排序 - Laravel order by distance from longitude latitude 使用纬度和经度获取最近的位置 - Get nearest locations using latitude and longitude 街道和点之间的距离(如果是经度和纬度) - Distance between street and point, if latitude and longitude 经纬度最近的位置 - Nearest locations with latitude and longitude 如何使用经度和纬度计算餐厅与该餐厅附近的 10 个司机之间的距离? - How to calculate the distance between a restaurant and 10 drivers who are nearby to that restaurant restaurant using longitude and latitude? 使用 PHP 和谷歌地图 Api 计算出 2 个纬度和经度之间的距离 - Using PHP and google Maps Api to work out distance between 2 latitude and longitude 使用Google Maps Api(JSON)获取两个位置之间的距离 - Getting the distance between two locations using Google Maps Api (JSON)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM