- Install Geocoder Laravel: https://geocoder-php.org/docs/
- Publish config
php artisan vendor:publish
(Look for Geocoder) - Setup your Google Geocoding API-Key in
config/geocoder.php
Last active
November 19, 2024 21:00
-
-
Save pxlrbt/7b3b0896db361bae38b22c5cadfd2335 to your computer and use it in GitHub Desktop.
Filament Geocoder
This file contains 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 | |
Forms\Components\Select::make('geocoding') | |
->label('Search') | |
->searchable() | |
->reactive() | |
->dehydrated(false) | |
->getSearchResultsUsing(function ($query) { | |
return app('geocoder')->geocode($query)->get() | |
->mapWithKeys(fn ($result) => [ | |
$result->getFormattedAddress() => $result->getFormattedAddress() | |
]) | |
->toArray(); | |
}) | |
->afterStateUpdated(function ($state, $set) { | |
/** @var \Geocoder\Provider\GoogleMaps\Model\GoogleAddress $result */ | |
$result = app('geocoder')->geocode($state)->get()->first(); | |
$coords = $result->getCoordinates(); | |
$set('street', $result->getStreetName()); | |
$set('street_number', $result->getStreetNumber()); | |
$set('city', $result->getLocality()); | |
$set('zipcode', $result->getPostalCode()); | |
$set('latitude', $coords->getLatitude()); | |
$set('longitude', $coords->getLongitude()); | |
}), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment