Skip to content

Instantly share code, notes, and snippets.

@ManojKiranA
Forked from pxlrbt/Readme.md
Created May 27, 2022 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ManojKiranA/2939201fb4d4f1f6f6731595d01e00d0 to your computer and use it in GitHub Desktop.
Save ManojKiranA/2939201fb4d4f1f6f6731595d01e00d0 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