Skip to content

Instantly share code, notes, and snippets.

@pxlrbt
Last active November 19, 2024 21:00
Show Gist options
  • Save pxlrbt/7b3b0896db361bae38b22c5cadfd2335 to your computer and use it in GitHub Desktop.
Save pxlrbt/7b3b0896db361bae38b22c5cadfd2335 to your computer and use it in GitHub Desktop.
Filament Geocoder

Usage

  • 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
<?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