sanderdewijs/lara-livewire-maps
composer require sanderdewijs/lara-livewire-maps
@LwMapsScripts directive to your layout (once per page, after <body>):
@LwMapsScripts
php artisan make:livewire MapDemo
<livewire:map-demo />
use Sanderdewijs\LaraLivewireMaps\LwMaps;
public function mount() {
$this->apiKey = env('GOOGLE_MAPS_API_KEY');
$this->center = [52.3676, 4.9021]; // Amsterdam coordinates
$this->zoom = 12;
}
public function render() {
return view('livewire.map-demo', [
'map' => LwMaps::make()
->apiKey($this->apiKey)
->center($this->center)
->zoom($this->zoom)
]);
}
$this->markers = [
['lat' => 52.3676, 'lng' => 4.9021, 'title' => 'Amsterdam'],
];
LwMaps builder:
$map = LwMaps::make()
->apiKey($this->apiKey)
->center($this->center)
->zoom($this->zoom)
->markers($this->markers);
<div wire:ignore>
{{ $map }}
</div>
LwMaps builder to configure the map and markers.
$map = LwMaps::make()
->apiKey($this->apiKey)
->center($this->center)
->zoom($this->zoom)
->markers($this->markers)
->height('500px');
<div wire:ignore>
{{ $map }}
</div>
public function updatedMarkerEvent($event) {
if ($event === 'marker-click') {
$this->dispatch('markerClicked', data: $event['marker']);
}
}
@this.on('markerClicked', (object) $marker => {
alert(`Marker clicked: ${marker.title}`);
})
$map = LwMaps::make()
->apiKey($this->apiKey)
->enableDrawingTools()
->onSelectionComplete(function ($selection) {
$this->dispatch('selectionComplete', data: $selection);
});
@this.on('selectionComplete', (object) $selection => {
console.log('Selection complete:', $selection);
})
$map = LwMaps::make()
->apiKey($this->apiKey)
->enableMarkerClustering()
->markers($this->markers);
public $markers = [];
public function addMarker($lat, $lng) {
$this->markers[] = ['lat' => $lat, 'lng' => $lng];
}
render():
$map = LwMaps::make()
->apiKey($this->apiKey)
->markers($this->markers);
markers array:
$this->markers = [
['lat' => 52.3676, 'lng' => 4.9021, 'icon' => 'path/to/icon.png'],
];
public function updatedMarkers() {
$this->map = LwMaps::make()
->apiKey($this->apiKey)
->markers($this->markers);
}
.env:
GOOGLE_MAPS_API_KEY=your_api_key_here
$this->apiKey = env('GOOGLE_MAPS_API_KEY');
@LwMapsScripts Directive@LwMapsScripts directive is missing or placed incorrectly.<body> in your layout.LwMaps builder:
LwMaps::make()->apiKey($this->apiKey)
config/lara-livewire-maps.php (if using the optional config).lat/lng) will cause rendering errors.$markers = [
['lat' => 52.3676, 'lng' => 4.9021, 'title' => 'Optional Title'],
];
marker-click) may conflict with Livewire's internal events.$map->onMarkerClick(function ($marker) {
$this->dispatch('map.marker-clicked', data: $marker);
});
F12) to debug JavaScript errors, especially if the map fails to load.Google Maps API error: MissingKey → API key not provided.Google Maps API error: RefererNotAllowed → Domain not whitelisted.dd() or logger() to inspect events:
public function updatedMarkerEvent($event) {
\Log::info('Marker event:', $event);
}
LwMaps builder returns a renderable object. Inspect it in render():
\Log::info('Map config:', $map->toArray());
$map = LwMaps::make()
->apiKey($this->apiKey)
->styles([
'featureType' => 'poi',
'elementType' => 'labels',
'stylers' => ['visibility' => 'off'],
]);
onMapReady callback to add custom overlays:
$map->onMapReady(function ($map) {
new google.maps.Circle({
map: $map,
center: { lat: 52.3676, lng: 4.9021 },
radius: 1000,
});
});
places, drawing) via the @LwMapsScripts directive:
@LwMapsScripts(['libraries' => ['places',
How can I help you explore Laravel packages today?