PHP code example of mayaram / laravel-browser-location
1. Go to this page and download the library: Download mayaram/laravel-browser-location library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mayaram / laravel-browser-location example snippets
'auto_save' => false, // persist every capture automatically
'min_accuracy' => 200, // max accepted accuracy in metres
'prevent_duplicates' => true, // skip saves within 20 m of last point
'default_collection' => 'default',
'component' => [
'auto_capture' => true,
'force_permission' => true,
'watch' => false,
'livewire_method' => 'setBrowserLocation',
],
use Mayaram\BrowserLocation\Contracts\Geocoder;
class CheckoutController
{
public function __construct(private readonly Geocoder $geocoder) {}
public function __invoke(): array
{
return $this->geocoder->reverse(28.6139, 77.2090);
}
}
namespace App\Livewire;
use Livewire\Component;
use Mayaram\BrowserLocation\Livewire\Concerns\InteractsWithBrowserLocation;
class TripTracker extends Component
{
use InteractsWithBrowserLocation;
public string $status = 'Waiting for location…';
// Called automatically after every location update
public function onBrowserLocationUpdated(array $location): void
{
$this->status = "Lat: {$this->getLatitude()}, Lng: {$this->getLongitude()}";
}
// Optional: return the model that locations should be attached to
public function getBrowserLocationable(): ?\Illuminate\Database\Eloquent\Model
{
return auth()->user();
}
public function render()
{
return view('livewire.trip-tracker');
}
}
public function onBrowserLocationUpdated(array $location): void
{
// $location has the same keys as $this->browserLocation
$this->dispatch('location-saved');
}
use Livewire\Attributes\Locked;
use Livewire\Attributes\On;
// Prevents the client from tampering with $browserLocation via wire:model
#[Locked]
public array $browserLocation = [];
// Allows triggering setBrowserLocation via $dispatch('browser-location:updated', payload)
// in addition to the default direct component.call() mechanism
#[On('browser-location:updated')]
public function setBrowserLocation(array $location): void
{
// Call the trait's implementation:
$this->persistBrowserLocationIfNeeded($location);
$this->browserLocation = $location;
if (method_exists($this, 'onBrowserLocationUpdated')) {
$this->onBrowserLocationUpdated($location);
}
}
#[On('browser-location:updated')]
public function setBrowserLocation(array $location): void { ... }