如何在每个结果上显示距离?

问题描述 投票:0回答:1

我正在尝试显示每个结果的距离。我在地址表中有纬度和经度,该表以 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'));
    }

我的刀片代码

@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

我可能以错误的方式来解决这个问题,但是任何关于如何调整它以显示场地上正确距离的帮助都将是一个巨大的帮助。

php laravel distance
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.