The package dispatches events to Livewire and listens for specific commands. This allows you to interact with the map from your PHP component seamlessly.
Listen for these events in your Livewire component:
| Event | Payload | Description |
|---|---|---|
| map:loaded | — | Map style is fully loaded |
| map:click | lat, lng, detail | User clicked on the map |
| map:zoom-changed | zoom | Zoom level changed |
| map:center-changed | lat, lng | Map center changed |
| map:marker-clicked | id, lat, lng | A marker was clicked |
| map:marker-drag-end | id, lat, lng | A draggable marker was released |
Trigger these from your Livewire component using $this->dispatch():
| Command | Description |
|---|---|
| map:fly-to | Move the map smoothly to a new location |
| map:set-zoom | Change zoom level |
| map:fit-bounds | Fit the map view to specific coordinates |
| map:resize | Force a map recalculation (useful for show/hide containers) |
Smoothly animate the map to a new location from your Livewire component:
use Livewire\Component; class FlightTracker extends Component { public function centerOnPilot() { $this->dispatch('map:fly-to', [ 'center' => [-0.09, 51.5], 'zoom' => 12, 'essential' => true, ]); } public function render() { return view('livewire.flight-tracker'); } }
<div> <button wire:click="centerOnPilot"> Center on Pilot </button> <x-map :center="[-0.09, 51.5]" :zoom="8" height="400px" > <x-map-controls /> </x-map> </div>
By default the map follows the .dark class on your <html> element when theme="auto" is set. You can also force a specific theme:
theme="light"
theme="dark"
Track and control the map viewport (center, zoom, bearing, pitch) by listening to events and using Alpine.js for real-time reactivity.
| Event | Detail | Description |
|---|---|---|
| map:move | lat, lng | Fired continuously as the map moves |
| map:center-changed | lat, lng | Fired once movement ends |
| map:zoom | zoom | Fired continuously during zoom |
| map:zoom-changed | zoom | Fired once zoom ends |
| map:bearing-changed | bearing | Fired when the map is rotated |
| map:pitch-changed | pitch | Fired when the map is tilted |
Automatically fit the map to show all your markers:
public function showAllLocations() { $this->dispatch('map:fit-bounds', [ 'bounds' => [ [-0.15, 51.49], // Southwest corner [lng, lat] [-0.01, 51.54], // Northeast corner [lng, lat] ], 'padding' => 50, ]); }
Programmatically change the zoom level:
public function zoomIn() { $this->dispatch('map:set-zoom', [ 'zoom' => 16, ]); }
Force the map to recalculate its size when the container changes (e.g. tabs, accordions, modals):
public function onTabChanged() { $this->dispatch('map:resize'); }
Interact with the map directly from any DOM element using Alpine directives:
| Directive | Value | Description |
|---|---|---|
| x-map-fly-to | center, zoom | Smoothly move the map on click |
| x-map-fit-bounds | bounds | Fit map to coordinates on click |
| x-map-set-zoom | int | Change zoom level on click |
| x-map-resize | '' | Trigger map resize on click |
The package includes a support class to easily convert PHP arrays or models into GeoJSON FeatureCollections:
use Kwasii\LivewireMapcn\Support\GeoJSON; $data = [ ['name' => 'London', 'lat' => 51.5, 'lng' => -0.1], ['name' => 'Paris', 'lat' => 48.8, 'lng' => 2.3], ]; $geojson = GeoJSON::fromArray($data); // Returns a valid GeoJSON array structure
Tune :cluster-radius (higher is faster) and :cluster-max-zoom to balance performance and detail.
Increase :tolerance (e.g., 1.0) to simplify GeoJSON geometry or :buffer to reduce off-screen flashing.
Use Alpine's .throttle or .debounce when listening to continuous events like @map:move.
Use the map:update-cluster-data-{id} event for real-time filtering instead of refreshing the whole component.