有什么方法可以找到半径为 laravel 的位置
Is there any way to find location with in radius in laravel
如何根据一个点的经纬度和给定的半径找到基于半径的位置。
假设,我给了 30 作为半径,它会在 30 半径内找到特定给定纬度和经度的结果。
我试过下面的代码:
$lat = '37.421998'; // latitude of centre of bounding circle in degrees
$lon = '-122.084000'; // longitude of centre of bounding circle in degrees
$rad = 30; // radius of bounding circle in kilometers
$R = 6371; // earth's mean radius, km
// first-cut bounding box (in degrees)
$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
$maxLon = $lon + rad2deg(asin($rad/$R) / cos(deg2rad($lat)));
$minLon = $lon - rad2deg(asin($rad/$R) / cos(deg2rad($lat)));
$results = DB::select('Select id, latitude, longitude, acos(sin(:lat)*sin(radians(latitude)) + cos(:lat)*cos(radians(latitude))*cos(radians(longitude)-(:lon))) * (:R) As D From ( Select id, latitude, longitude From jobs_master Where latitude Between (:minLat) And (:maxLat) And longitude Between (:minLon) And (:maxLon) ) As FirstCut Where acos(sin(:lat)*sin(radians(latitude)) + cos(:lat)*cos( radians(latitude))*cos(radians(longitude)-(:lon))) * :R < :rad ', ['maxLat' => $maxLat,'maxLon' => $maxLon, 'minLat' => $minLat,'minLon' => $minLon, 'lat' => $lat,'lon' => $lon,'R'=> $R,'rad'=>$rad]);
dd(DB::getQueryLog());exit;
print_r($results);exit;
谁能帮帮我
您可以使用以下计算并根据需要修改查询:
$query = "SELECT (3956 * 2 * ASIN(SQRT( POWER(SIN(( $latitude - latitude) * pi()/180 / 2), 2) + COS( $latitude * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( $longitude - longitude) * pi()/180 / 2), 2) ))) as distance, latitude, longitude" FROM TABLES HAVING distance <= 30
所以在上面的查询中我们首先通过给定的纬度和经度计算距离,然后使用 HAVING 子句将距离限制为 30 公里半径。
希望这能解决您的问题:)
这是使用 Eloquent 查询解决 Laravel 问题的一个很好的解决方案,此方法是为不同的场景编写的,但正是您要找的东西:
private function findNearestRestaurants($latitude, $longitude, $radius = 400)
{
/*
* using eloquent approach, make sure to replace the "Restaurant" with your actual model name
* replace 6371000 with 6371 for kilometer and 3956 for miles
*/
$restaurants = Restaurant::selectRaw("id, name, address, latitude, longitude, rating, zone ,
( 6371000 * acos( cos( radians(?) ) *
cos( radians( latitude ) )
* cos( radians( longitude ) - radians(?)
) + sin( radians(?) ) *
sin( radians( latitude ) ) )
) AS distance", [$latitude, $longitude, $latitude])
->where('active', '=', 1)
->having("distance", "<", $radius)
->orderBy("distance",'asc')
->offset(0)
->limit(20)
->get();
return $restaurants;
}
如何根据一个点的经纬度和给定的半径找到基于半径的位置。 假设,我给了 30 作为半径,它会在 30 半径内找到特定给定纬度和经度的结果。 我试过下面的代码:
$lat = '37.421998'; // latitude of centre of bounding circle in degrees
$lon = '-122.084000'; // longitude of centre of bounding circle in degrees
$rad = 30; // radius of bounding circle in kilometers
$R = 6371; // earth's mean radius, km
// first-cut bounding box (in degrees)
$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
$maxLon = $lon + rad2deg(asin($rad/$R) / cos(deg2rad($lat)));
$minLon = $lon - rad2deg(asin($rad/$R) / cos(deg2rad($lat)));
$results = DB::select('Select id, latitude, longitude, acos(sin(:lat)*sin(radians(latitude)) + cos(:lat)*cos(radians(latitude))*cos(radians(longitude)-(:lon))) * (:R) As D From ( Select id, latitude, longitude From jobs_master Where latitude Between (:minLat) And (:maxLat) And longitude Between (:minLon) And (:maxLon) ) As FirstCut Where acos(sin(:lat)*sin(radians(latitude)) + cos(:lat)*cos( radians(latitude))*cos(radians(longitude)-(:lon))) * :R < :rad ', ['maxLat' => $maxLat,'maxLon' => $maxLon, 'minLat' => $minLat,'minLon' => $minLon, 'lat' => $lat,'lon' => $lon,'R'=> $R,'rad'=>$rad]);
dd(DB::getQueryLog());exit;
print_r($results);exit;
谁能帮帮我
您可以使用以下计算并根据需要修改查询:
$query = "SELECT (3956 * 2 * ASIN(SQRT( POWER(SIN(( $latitude - latitude) * pi()/180 / 2), 2) + COS( $latitude * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( $longitude - longitude) * pi()/180 / 2), 2) ))) as distance, latitude, longitude" FROM TABLES HAVING distance <= 30
所以在上面的查询中我们首先通过给定的纬度和经度计算距离,然后使用 HAVING 子句将距离限制为 30 公里半径。
希望这能解决您的问题:)
这是使用 Eloquent 查询解决 Laravel 问题的一个很好的解决方案,此方法是为不同的场景编写的,但正是您要找的东西:
private function findNearestRestaurants($latitude, $longitude, $radius = 400)
{
/*
* using eloquent approach, make sure to replace the "Restaurant" with your actual model name
* replace 6371000 with 6371 for kilometer and 3956 for miles
*/
$restaurants = Restaurant::selectRaw("id, name, address, latitude, longitude, rating, zone ,
( 6371000 * acos( cos( radians(?) ) *
cos( radians( latitude ) )
* cos( radians( longitude ) - radians(?)
) + sin( radians(?) ) *
sin( radians( latitude ) ) )
) AS distance", [$latitude, $longitude, $latitude])
->where('active', '=', 1)
->having("distance", "<", $radius)
->orderBy("distance",'asc')
->offset(0)
->limit(20)
->get();
return $restaurants;
}