2016-11-26 3 views
4

私はLaravel 5.1でネリカムの店を取得しようとしています。私は座標を計算するジオコーディングパーサーを持っています。しかし、私はhaversineの式に問題があります。基本的に私はテーブルAziende(店舗)から緯度、長い電子カテゴリのトラフURLを渡し、近くの店をフェッチを指定する必要があります。Laravelの近くの店

私はこのコードを試してみてください。

$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(); 
} 
?> 
+0

使用しているデータベースとバージョンは何ですか? MySQLを使用している場合、ST_Distance ... v5.7にはST_Distance_Sphereがあります https://dev.mysql.com/doc/refman/5.7/en/spatial-convenience-functions.html – jcorry

答えて

0

あなたはSQLクエリで半正矢式を実装するために、次の動作するはずです。私の答えは、あなたがデータベーステーブル "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();