Laravel 附近的商店

Nearby stores in Laravel

我正在尝试在 Laravel 5.1 中获取 neraby 存储 我有计算坐标的地理编码解析器。但是我对 haversine 公式有疑问。基本上我需要从 table Aziende(商店)给定经纬度类别通过 url,获取附近的商店。

我试试这个代码:

$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 查询中实现半正弦公式。我的回答是假设您有一个与数据库对应的模型设置 table "aziende".

$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();