Laravel 拉维尔附近的商店
我试图在Laravel5.1中获得neraby存储,我有caluclate协作的地理编码解析器。但我对哈弗森公式有疑问。基本上,我需要从表Aziende(Stores)中获取,给定一个通过url传递的lat、long e类别,获取附近的商店 我尝试使用以下代码:Laravel 拉维尔附近的商店,laravel,eloquent,laravel-5.1,geocoding,haversine,Laravel,Eloquent,Laravel 5.1,Geocoding,Haversine,我试图在Laravel5.1中获得neraby存储,我有caluclate协作的地理编码解析器。但我对哈弗森公式有疑问。基本上,我需要从表Aziende(Stores)中获取,给定一个通过url传递的lat、long e类别,获取附近的商店 我尝试使用以下代码: $dove = Input::get('dove'); $categoria_id = Input::get('id_categoria'); // 4: check if any matches found in th
$dove = Input::get('dove');
$categoria_id = Input::get('id_categoria');
// 4: check if any matches found in the database table
if (!empty($dove)) {
$response = \GoogleMaps::load('geocoding')->setParamByKey ('address', $dove)->get();
$response = json_decode($response, true);
$latitude = $response['results'][0]['geometry']['location']['lat'];
$longitude = $response['results'][0]['geometry']['location']['lng'];
$radius = '5';
$aziende = DB::table('aziende')
->select(
( 'lat * acos( cos( radians(50) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) )
+ sin( radians(37) ) * sin( radians( lat ) ) ) as distance') )->get();
} else {
$aziende = DB::table("aziende")->where('categoria', $categoria_id)->get();
}
?>
要在SQL查询中实现haversine公式,以下内容应该很有用。我的答案是假设您有一个与数据库表“aziende”对应的模型设置
$lat=floatval($response['results'][0]['geometry']['location']['lat']);
$lng=floatval($response['results'][0]['geometry']['location']['lng']);
$radius=5;
$distance=DB::原始(“*,(6371*acos(弧度($lat))*cos(弧度(lat))*cos(弧度(lng)-弧度($lng))+sin(弧度($lat))*sin(弧度(lat))作为距离”);
$aziende=aziende::select($distance)->orderBy('distance')->where('distance',您使用的是什么数据库/版本?如果您使用的是MySQL,您就有ST_distance…v5.7有ST_distance_Sphere
$lat = floatval($response['results'][0]['geometry']['location']['lat']);
$lng = floatval($response['results'][0]['geometry']['location']['lng']);
$radius = 5;
$distance = DB::raw("*, ( 6371 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance");
$aziende = Aziende::select($distance)->orderBy('distance')->where('distance', '<=', $radius)->get();