Skip to content

Instantly share code, notes, and snippets.

@vmitchell85
Created December 6, 2025 20:19
Show Gist options
  • Select an option

  • Save vmitchell85/5b23cdd2c7bb92f7f7808bea7e961e37 to your computer and use it in GitHub Desktop.

Select an option

Save vmitchell85/5b23cdd2c7bb92f7f7808bea7e961e37 to your computer and use it in GitHub Desktop.
<?php
namespace App\Traits;
use Native\Mobile\Attributes\OnNative;
use Native\Mobile\Events\Geolocation\LocationReceived;
use Native\Mobile\Events\Geolocation\PermissionRequestResult;
use Native\Mobile\Events\Geolocation\PermissionStatusReceived;
use Native\Mobile\Facades\Geolocation;
trait HandlesGeolocation
{
public $latitude = null;
public $longitude = null;
public $accuracy = null;
public $locationError = null;
public $permissionStatus = 'not_determined';
public $isRequestingLocation = false;
public $useFineAccuracy = false;
public function initAndFetchLocation($useFineAccuracy = false)
{
$this->useFineAccuracy = $useFineAccuracy;
$this->checkLocationPermissions();
}
public function checkLocationPermissions()
{
Geolocation::checkPermissions();
}
public function requestLocationPermissions()
{
$this->isRequestingLocation = true;
Geolocation::requestPermissions();
}
public function getCurrentLocation($fineAccuracy = false)
{
$this->useFineAccuracy = $fineAccuracy;
$this->isRequestingLocation = true;
$this->locationError = null;
Geolocation::getCurrentPosition($fineAccuracy);
}
#[OnNative(PermissionStatusReceived::class)]
public function handlePermissionStatus($location, $coarseLocation, $fineLocation)
{
$this->permissionStatus = $location;
// If permissions are granted and this is the initial check, get location
if (($location === 'granted' || $coarseLocation === 'granted' || $fineLocation === 'granted') && ! $this->isRequestingLocation) {
$this->getCurrentLocation($this->useFineAccuracy);
} elseif (($location === 'not_determined' || $location === 'denied') && ! $this->isRequestingLocation) {
// Request permissions if not yet determined
$this->requestLocationPermissions();
}
}
#[OnNative(PermissionRequestResult::class)]
public function handlePermissionRequest($location, $coarseLocation, $fineLocation, $message = null, $needsSettings = null)
{
$this->permissionStatus = $location;
if ($location === 'permanently_denied') {
$this->locationError = 'Location permission permanently denied. Please enable in Settings.';
$this->isRequestingLocation = false;
// Call the failure callback if it exists
if (method_exists($this, 'locationFailed')) {
$this->locationFailed($this->locationError);
}
} elseif ($coarseLocation === 'granted' || $fineLocation === 'granted') {
// Permission granted, get location
$this->getCurrentLocation($this->useFineAccuracy);
} else {
$this->locationError = 'Location permission is required for this feature.';
$this->isRequestingLocation = false;
// Call the failure callback if it exists
if (method_exists($this, 'locationFailed')) {
$this->locationFailed($this->locationError);
}
}
}
#[OnNative(LocationReceived::class)]
public function handleLocationReceived(
$success = null,
$latitude = null,
$longitude = null,
$accuracy = null,
$timestamp = null,
$provider = null,
$error = null
) {
$this->isRequestingLocation = false;
if ($success) {
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->accuracy = $accuracy;
$this->locationError = null;
// Call the success callback if it exists
if (method_exists($this, 'locationReceived')) {
$this->locationReceived($latitude, $longitude, $accuracy, $timestamp, $provider);
}
} else {
$this->locationError = $error ?? 'Failed to get location.';
// Call the failure callback if it exists
if (method_exists($this, 'locationFailed')) {
$this->locationFailed($this->locationError);
}
}
}
public function hasLocationPermission()
{
return in_array($this->permissionStatus, ['granted']);
}
public function hasLocation()
{
return $this->latitude !== null && $this->longitude !== null;
}
public function clearLocation()
{
$this->latitude = null;
$this->longitude = null;
$this->accuracy = null;
$this->locationError = null;
}
}
@vmitchell85
Copy link
Author

Example usage:

<?php

namespace App\Livewire;

use App\Traits\HandlesGeolocation;
use Livewire\Component;

class HomePage extends Component
{
    use HandlesGeolocation;

    // Success callback - called when location is received
    public function locationReceived($latitude, $longitude, $accuracy, $timestamp, $provider)
    {
        // Handle successful location retrieval
        ray('Location received', [
            'latitude' => $latitude,
            'longitude' => $longitude,
            'accuracy' => $accuracy,
            'timestamp' => $timestamp,
            'provider' => $provider,
        ]);
    }

    // Failure callback - called when location fails or permission denied
    public function locationFailed($error)
    {
        // Handle location failure
        ray('Location failed', ['error' => $error]);
    }

    public function refreshLocation()
    {
        // Method to manually refresh location with fine accuracy
        $this->initAndFetchLocation();
    }

    public function render()
    {
        return view('livewire.home-page');
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment