我正在尝试显示每个结果的距离。我在地址表中具有纬度和经度,该地址表以1比1的比例连接到场所表。
我已经设法做到了,以便将所有距离都排列在一个数组中,但是当我尝试将它们添加到场地中时,它只是列表中的最后一个。如果我在它们周围包裹一个foreach,那么它将显示每个场所的所有距离。
我的控制器代码
public function venues(Request $request)
{
$types = DB::table('types')
->join('venues', 'venues.type_id', '=', 'types.id')
->join('icons', 'types.icon_id', '=', 'icons.id')
->where('venues.is_approved', '=', 1)
->select('types.*', 'icons.*')
->groupBy('types.id')
->get();
$type = $request->query('type');
if ($type) {
$venues = Venue::whereHas('type', function (Builder $query) use ($type) {
$query->whereIn('name', $type);
})->where('is_approved', '=', 1)->get();
} else {
$venues = Venue::where('is_approved', '=', '1')->get();
}
if (auth()->user()->address) {
foreach ($venues as $venue) {
$earthRadius = '3959';
$latFrom = deg2rad(auth()->user()->address->address_latitude);
$lonFrom = deg2rad(auth()->user()->address->address_longitude);
$latTo = deg2rad($venue->address->address_latitude);
$lonTo = deg2rad($venue->address->address_longitude);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
$distance[] = $angle * $earthRadius;
}
} else {
$distance = '';
}
return view('venue.venues', compact('venues', 'types', 'distance'));
}
我的Blade代码
@foreach ($venues as $venue)
<div class="venue-card">
<a href="{{ url('venue/' . $venue->id .'/'. str_replace(' ', '-', $venue->name)) }}">
@if (!$venue->photos->count() == 0)
@foreach ($venue->photos->take(1) as $photo)
<div class="main_image" style="background-image: url({{ asset('/uploads/venues/thumbnails/'.$photo->image_url)}});"></div>
@endforeach
@else
<div class="main_image noimage" style="background-image: url({{ asset('/assets/images/no-photos.svg')}});"></div>
@endif
<div class="venue-overview">
@if ($venue->type->icon_id)
<img class="icon" src="{{ asset($venue->type->icon->icon_url) }}">
@endif
<span> {{$venue->type->name}}</span>
<h3>{{$venue->name}}</h3>
<p>{{$venue->description}}</p>
@if (!$distance == '')
<p><span>{{round($distance, 0)}}</span> miles from your address</p>
@endif
@auth
<div class="fav_venue">
<favorite
:venue={{ $venue->id }}
:favorited={{ $venue->favorited() ? 'true' : 'false' }}
></favorite>
</div>
@endauth
</div>
</a>
</div>
@endforeach
我可能走错了路,但是任何有关如何使它适应场地上正确距离的帮助都会有很大帮助。
答案 0 :(得分:0)
因此,经过几个小时的游戏,我已经做到了,现在可以正常工作了:
if (auth()->user()->address) {
foreach ($venues as $key => $venue) {
$earthRadius = '3959';
$latFrom = deg2rad(auth()->user()->address->address_latitude);
$lonFrom = deg2rad(auth()->user()->address->address_longitude);
$latTo = deg2rad($venue->address->address_latitude);
$lonTo = deg2rad($venue->address->address_longitude);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
$venue->distance = $angle * $earthRadius;
}
} else {
$venues->distance = '';
}
$venues = $venues->sortBy('distance');
$venues->values()->all();