romkaltu/livewire-google-places-autocomplete
Install the package via Composer:
composer require romkaltu/livewire-google-places-autocomplete
Publish the configuration (optional, for API key setup):
php artisan vendor:publish --provider="RomkaLTU\LivewireGooglePlacesAutocomplete\LivewireGooglePlacesAutocompleteServiceProvider"
First Use Case: Add the component to a Livewire form for address input. Start with a basic implementation in your Livewire component:
use RomkaLTU\LivewireGooglePlacesAutocomplete\LivewireGooglePlacesAutocomplete;
public function render()
{
return view('livewire.your-component', [
'places' => LivewireGooglePlacesAutocomplete::make('address')
->apiKey(config('services.google_maps.api_key'))
->language('en')
->types(['(cities)'])
->debounce(500)
]);
}
Include the component in your Blade view:
<div>
{{ $places }}
</div>
Use the component as a replacement for standard text inputs in forms:
// In your Livewire component
public $address;
public function mount()
{
$this->places = LivewireGooglePlacesAutocomplete::make('address')
->apiKey(config('services.google_maps.api_key'))
->onChange(function ($value) {
$this->address = $value;
});
}
Extend functionality by handling selected places:
$places = LivewireGooglePlacesAutocomplete::make('address')
->onSelect(function ($place) {
// Extract structured data (e.g., lat/lng, formatted address)
$this->latitude = $place['geometry']['location']['lat'];
$this->longitude = $place['geometry']['location']['lng'];
$this->formattedAddress = $place['formatted_address'];
});
Use environment variables or config files for security:
// config/services.php
'google_maps' => [
'api_key' => env('GOOGLE_MAPS_API_KEY'),
],
Then reference it in your component:
->apiKey(config('services.google_maps.api_key'))
Integrate with Laravel validation rules:
use Illuminate\Validation\Rule;
protected $rules = [
'address' => ['required', Rule::unique('locations')->ignore($this->locationId)],
];
public function updated($property)
{
$this->validateOnly($property);
}
Bind multiple fields to different place properties:
$places = LivewireGooglePlacesAutocomplete::make('address')
->onSelect(function ($place) {
$this->address = $place['formatted_address'];
$this->city = $place['address_components'][0]['long_name'] ?? null;
$this->country = $place['address_components'][1]['long_name'] ?? null;
});
Show/hide the component based on logic:
@if ($showAddressField)
{{ $places }}
@endif
API Key Restrictions:
Debounce Delays:
->debounce(300) // Faster response, but more API calls
CORS Issues:
localhost. Add http://localhost to the Restrictions > Allowed referers in the Google Cloud Console.Rate Limits:
Livewire State Persistence:
public properties for fields that should persist.Check Network Requests:
Open Chrome DevTools (F12) and inspect the XHR tab for failed API calls. Look for 403 Forbidden errors (often API key issues) or 500 errors (server-side problems).
Log Place Data: Debug selected places by logging them:
->onSelect(function ($place) {
\Log::info('Selected place:', $place);
})
Fallback for No API Key: Provide a user-friendly fallback (e.g., a text input) if the API key is missing:
@if(config('services.google_maps.api_key'))
{{ $places }}
@else
<input type="text" wire:model="address" placeholder="Enter address manually">
@endif
Custom Templates: Override the default Blade template by publishing and modifying:
php artisan vendor:publish --tag=livewire-google-places-autocomplete-views
Edit resources/views/vendor/livewire-google-places-autocomplete.blade.php.
Add Place Markers: Extend the component to display markers on a map (e.g., using Leaflet or Google Maps JS API):
->onSelect(function ($place) {
$this->emit('placeSelected', $place);
});
Then listen in JavaScript:
Livewire.on('placeSelected', place => {
// Initialize map and add marker
});
Localization: Support multiple languages dynamically:
->language($this->user->preferredLanguage ?? 'en')
Autocomplete Types: Use Google’s autocomplete types for granular control:
->types(['geocode', 'establishment', 'address'])
Component Events: Trigger custom events for integration with other Livewire components:
->onSelect(function ($place) {
$this->emit('addressUpdated', $place['formatted_address']);
});
How can I help you explore Laravel packages today?