php - 如何使用搜索功能显示具有最接近的 long/lat 值的商店 - Laravel 5 eloquent

标签 php google-maps laravel-5.1

理想情况下,我正在寻找与 Laravel 5 相关的答案。我正在尝试制作商店定位器应用程序。我正在尝试将一对纬度/经度坐标(根据用户在搜索框中输入的地址计算得出)与最近(半径 100 公里以内)的坐标/数据库中的现有商店进行匹配。

用户输入的地址被转换(使用地理编码)为纬度和经度坐标。这些被发送到我的“文章”页面,其中有商店列表和谷歌地图。我使用了一个关于范围搜索的简单教程来根据文本“地址”显示我的文章/商店。但这显然不适用于两个坐标。我有一个名为 Articles 的表,其中包含:id、地址、lat、lng、网站、标题等。

我需要像 this 这样的东西或 this但使用 Eloquent 。

当前文章模型:

    public function scopeSearch($query, $search){

    return $query->where('address', 'LIKE', "%$search%" );
   }

文章 Controller

 public function index(Request $request)
{
     $query = $request->get('q');
     $articles = $query
    ? Article::search($query)->get()
    : Article::all();
    return view('articles.index', compact('categories'))->withArticles($articles);
}

当前形式/地理编码:

    {!! Form::open(['method' => 'GET', 'id' => 'searchForm', 'name' => 'searchForm', 'route' => 'articles_path']) !!}
      {!! Form::input('search', 'q', null, ['placeholder' => 'LOCATION', 'class' => 'locationSearch', 'id' => 'searchInput'])!!}
      {!! Form::hidden('lat', null, ['id' => 'lat'])!!}
      {!! Form::hidden('lng', null, ['id' => 'lng'])!!}
      {!! Form::submit('MAKE ME HAPPY', array('id' => 'submitButton')) !!}
    {!! Form::close() !!}

 <script>
$('#submitButton').on('click', function(event){
    event.preventDefault();
    var address = $('#searchInput').val();
    geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
             var lat = results[0].geometry.location.lat();
             var lng = results[0].geometry.location.lng();
             $('#lat').val(lat);
             $('#lng').val(lng);
          $('#searchForm').submit();

        } else {
            alert("Geocode was not successful");
        }
    });
});
</script>  

最佳答案

我最终关注了 this advice .我不知道这是否是“ Eloquent ”的方式,但它确实有效。我只是不确定“距离”以及在那里使用什么。

文章模型:

public static function getByDistance($lat, $lng, $distance)
{
  $results = DB::select(DB::raw('SELECT id, ( 3959 * acos( cos( radians(' . $lat . ') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $lng . ') ) + sin( radians(' . $lat .') ) * sin( radians(lat) ) ) ) AS distance FROM articles HAVING distance < ' . $distance . ' ORDER BY distance') );

  return $results;
}

文章 Controller

    public function index(Request $request)
{

$lat = $request->get('lat');
$lng = $request->get('lng');
$distance = 1;

$query = Article::getByDistance($lat, $lng, $distance);

    if(empty($query)) {
      return view('articles.index', compact('categories'));
    }

    $ids = [];

    //Extract the id's
    foreach($query as $q)
    {
      array_push($ids, $q->id);
    }

    // Get the listings that match the returned ids
    $results = DB::table('articles')->whereIn( 'id', $ids)->orderBy('rating', 'DESC')->paginate(3);        

    $articles = $results;

    return view('articles.index', compact('categories'))->withArticles($articles);

}

关于php - 如何使用搜索功能显示具有最接近的 long/lat 值的商店 - Laravel 5 eloquent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34010916/

相关文章:

php - 在 Ubuntu 11.10 上使用 PHP 的谷歌地图有问题

php - 与 Laravel 5.1 一起使用时,getID3 "getid3_writetags"未找到类异常

javascript - 使用 jquery 在 php cURL 中搜索

php 将 string 转换为 int 没有给出正确的值

php - 在 PHP 中转换日期格式

PhpStorm $_POST 总是空的

google-maps - 嵌入谷歌地图会改变网站字体

javascript - 谷歌地图和异步 javascript

javascript - laravel Elixir 映射类似文件

php - Laravel Eloquent 返回 QueryException,未找到基表或 View