Created
December 6, 2025 20:19
-
-
Save vmitchell85/5b23cdd2c7bb92f7f7808bea7e961e37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: