附近的拉拉韦尔商店 [英] Nearby stores in Laravel

查看:123
本文介绍了附近的拉拉韦尔商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Laravel 5.1中获得neraby商店,我有一个地理编码解析器可以计算出coorinate。但是我有haversine公式的问题。基本上,我需要从表Aziende(商店)中获得给定的lat,long e类别并通过槽URL,然后获取附近的商店。

i'm try to get neraby store in Laravel 5.1 I have geocoding parser that caluclate coorinate. But i have problem with haversine formulas. Basically i need that from table Aziende (Stores) given a lat, long e category passed trough url, fetch the nearby stores.

我尝试使用以下代码:

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


推荐答案

以下应可帮助您实现Haversine公式在SQL查询中。我的答案是假设您有一个与数据库表 aziende相对应的模型设置。

The following should work for you to implement the haversine formula in an SQL query. My answer is assuming you have a model setup to correspond with the database 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();

这篇关于附近的拉拉韦尔商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆