问题描述
理想情况下,我正在寻找Laravel 5的相关答案。我正在尝试制作商店定位器应用程序。我试图将匹配一对经纬度坐标(从用户输入到搜索框中的地址计算出)与我的数据库中最接近的(在半径100公里范围内)坐标/现有商店进行匹配。 / p>用户输入一个地址(使用地理编码)转换为lat和lng坐标。这些被发送到我的'商品'页面,其中有一个商店和一个列表。谷歌地图。我使用了一个关于范围搜索的简单教程,根据他们的文本地址显示我的文章/商店。但是这显然不适用于两个坐标。我有一张名为Articles的表格,其中包含:id,address,lat,lng,website,title等。
我需要类似或但是使用雄辩。
当前文章模型:
public function scopeSearch($ query, $ search){
return $ query-> where('address','LIKE',%$ search%);
$ / code>
文章控制器
<$公共函数索引(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({$ b $'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(); $ b $('#lat')。val(lat);
$('#lng')。val(lng);
$('#searchForm' ).submit();
} else {
alert(Geocode was not successful);
}
});
});
< / script>
我结束了。我不知道这是否是'雄辩'的方式,但它是有效的。我只是不确定'距离'和在那里使用什么。
文章模型:
public static function getByDistance($ lat,$ lng,$ distance)
{
$ results = DB :: select(DB :: raw('SELECT id,(3959 * cos(弧度('。$ lat。'))* cos(弧度(lat))* cos(弧度(lng) - 弧度('。$ lng。'))+ sin(弧度('。$ lat 。'))* sin(radians(lat))))AS距距离距离<'。$距离。'ORDER BY距离'));
返回$结果;
$ / code>
文章控制器
<$公共函数索引(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 = [];
//提取id的
foreach($ query as $ q)
{
array_push($ ids,$ q-> id);
}
//获取匹配返回的id的列表
$ results = DB :: table('articles') - > whereIn('id',$ ids ) - > orderBy('rating','DESC') - > paginate(3);
$ articles = $ results;
return view('articles.index',compact('categories')) - > withArticles($ articles);
}
Ideally I am looking for an Laravel 5 relevant answer. I am trying to make a store locator app. I am up to the point where I'm trying to match one pair of lat/long coordinates (calculated from an address that a user enters into search box) with the nearest (within 100km radius) coordinates/existing stores in my database.
The user enters an address which is converted (using geocoding) into lat and lng coordinates. These are sent to my 'articles' page which has a list of stores & a google map. I used a simple tutorial about scope search to show my articles/stores based on their text 'address'. But this obviously doesn't work for two coordinates. I have a table called Articles which has: id, address, lat, lng, website, title etc..
I need something like this or this but using eloquent.
Current Article Model:
public function scopeSearch($query, $search){
return $query->where('address', 'LIKE', "%$search%" );
}
Articles 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);
}
Current form/ geocode:
{!! 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>
I ended up following this advice. I don't know if it's the 'eloquent' way, but it works. I'm just unsure about the 'distance' and what to use there.
Article model:
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;
}
Articles 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);
}
这篇关于如何使用搜索功能显示具有最接近的长/经度值的商店 - Laravel 5雄辩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!