【问题标题】:Laravel livewire - how to pass geo coordinates of user to livewire componentLaravel livewire - 如何将用户的地理坐标传递给 livewire 组件
【发布时间】:2020-08-05 11:27:42
【问题描述】:

我正在尝试将用户的地理坐标(纬度和经度)发送到 livewire 组件以加载附近的地点。但是我无法发送它,因为它们是 javascript 变量。问题是如何将 javascript 变量发送到 livewire 组件?

这是我到目前为止所做的。

# home.blade.php

@extends('layouts.app')

@section('content')
    <script>
        var latitude;
        var longitude;
        navigator.geolocation.getCurrentPosition(
            function success(pos) {
                var loc = pos.coords;
                latitude = loc.latitude;
                longitude = loc.longitude;
            }, 
            function error(err) {
                // console.warn(`ERROR(${err.code}): ${err.message}`);
                $.getJSON('https://ipinfo.io/geo', function(response) { 
                    var loc = response.loc.split(',');
                    latitude = parseFloat(loc[0]);
                    longitude = parseFloat(loc[1]);
                });
            }, options
        );
    </script>
    @livewire('nearby')
@endsection
@livewireScripts
# Nearby.php 

namespace App\Http\Livewire;

use Livewire\Component;
use Livewire\WithPagination;
use App\Listing;

class Nearby extends Component
{
    use WithPagination;

    public function render()
    {
        $listings = Listing::paginate(9);
        return view('livewire.nearby', [
            'listings' => $listings
        ]);
    }
}
# nearby.blade.php - livewire blade

<div class="relative bg-gray-50 px-4 sm:px-6 lg:px-8">
    <div class="relative max-w-6xl mx-auto">
        <div class="mt-12 grid gap-5 max-w-lg mx-auto lg:grid-cols-3 lg:max-w-none">
            @foreach($listings as $listing)
                @include('components.listing',['listing'=>$listing])
            @endforeach
        </div>
        <div class="text-center mx-auto mt-3">
            {{ $listings->links() }}
        </div>
    </div>
</div>

【问题讨论】:

    标签: php laravel laravel-livewire


    【解决方案1】:

    您需要从您的脚本全局发出一个事件。像这样:

     //emit this event inside your success function. pass latitude and longitude with the event 
    
      window.livewire.emit('set:latitude-longitude', latitude, longitude) 
    
    

    之后,您可以像这样从您的 livewire 组件类中收听此事件:

      protected $listeners = [
            'set:latitude-longitude' => 'setLatitudeLongitude'
        ];
    
    

    然后在您的 livewire 组件类中使用 setLatitudeLongitude 函数设置您传递的纬度。

    public function setLatitudeLongitude($latitude, $longitude) 
    {
       $this->latitude = $latitude;
       $this->longitude = $longitude;
    }
    

    如果您需要进一步解释,请告诉我:)

    【讨论】:

    • 谢谢@fahim152,我已经设法得到坐标。但是,我仍然停留在这个阶段。获得坐标后,我必须进行一些数据库查询以获取一些集合。如何将此结果发送到页面?
    • 在您的 livewire 组件类中获取一个公共属性,并将其设置为 null 或空数组。然后在您的 setLatitudeLongitude() 函数中查询,并将结果存储到您定义的公共属性中。你有它。 (为了使代码更具可读性,请调用另一个函数 fetchCoordinatesData(),它需要 lat long 并在内部进行查询!在 setLatLong 中调用该函数!)最后不要忘记将我的答案作为正确答案,如果您觉得有帮助,请竖起大拇指/解决你的问题:D
    猜你喜欢
    • 2021-09-02
    • 2020-12-14
    • 2022-01-16
    • 2021-06-22
    • 1970-01-01
    • 2023-01-06
    • 2020-10-23
    • 2022-11-17
    • 2020-06-24
    相关资源
    最近更新 更多