Map
A map with markers, built with Leaflet and OpenStreetMap; it can write the clicked location to a Livewire property.
zb:map zubazar/ui-pro
Needs a Leaflet registration and, in production, your own tile provider: see Setup.
This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →
Basic usage
Set the centre with center ([latitude, longitude]) and the zoom with zoom; the default centre is Istanbul.
<zb:map :center="[41.0082, 28.9784]" :zoom="14" :height="280" />
| Prop | Values |
|---|---|
center | [latitude, longitude] |
zoom | 1–19 (higher zooms in) |
height | Pixels (default 360) |
Markers
markers is an array of items with lat, lng and an optional title; with several markers the map fits them all.
<zb:map :markers="[
['lat' => 40.9909, 'lng' => 29.0286, 'title' => 'Kadıköy warehouse'],
['lat' => 41.0430, 'lng' => 29.0094, 'title' => 'Beşiktaş store'],
['lat' => 41.0256, 'lng' => 29.0963, 'title' => 'Ümraniye warehouse'],
['lat' => 40.9819, 'lng' => 28.8772, 'title' => 'Bakırköy store'],
]" />
| Prop | Values |
|---|---|
markers | Array of ['lat', 'lng', 'title'] |
Picking a location
With pick, a click puts a marker there and sends the map-picked event; click the map to try it.
<div x-data="{ location: null }" x-on:map-picked="location = $event.detail">
<zb:map pick="location" :height="280" />
<p class="mt-3 text-sm text-dim" x-text="location ? 'Selected location: ' + location.lat.toFixed(5) + ', ' + location.lng.toFixed(5) : 'Click a point on the map.'"></p>
</div>
| Prop | Values |
|---|---|
pick | Livewire property the location is written to |
map-picked | Event dispatched on click: { lat, lng } |
With Livewire
In Livewire the clicked location is written to the property you give pick, as ['lat' => …, 'lng' => …].
{{-- In the component: public ?array $location = null; --}}
<zb:map pick="location" :center="[$warehouse->lat, $warehouse->lng]" :zoom="16" :markers="[['lat' => $warehouse->lat, 'lng' => $warehouse->lng, 'title' => $warehouse->name]]" />
@if ($location)
<p class="mt-2 text-sm">New location: {{ $location['lat'] }}, {{ $location['lng'] }}</p>
@endif
| Prop | Values |
|---|---|
pick | Name of the Livewire property |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
center | — | [41.0082, 28.9784] | [latitude, longitude] |
zoom | number | 12 | Zoom level (up to 19) |
markers | — | [] | Array of ['lat', 'lng', 'title']; with more than one, the map is fitted to all of them |
height | number | 360 | Map height (pixels) |
pick | — | null | Livewire property the clicked location is written to as ['lat', 'lng'] |
registerMap: tileUrl | — | OpenStreetMap | Tile URL template |
registerMap: attribution | — | © OpenStreetMap | Attribution text |
Installation
Install Leaflet and register it in resources/js/app.js with your own tile provider.
// npm install leaflet
// resources/js/app.js
document.addEventListener('livewire:init', () => {
const { lazyLibrary, registerMap } = window.ZubazarUI.pro;
registerMap(window.Alpine, lazyLibrary(() => import('leaflet/dist/leaflet.css').then(() => import('leaflet'))), {
tileUrl: 'https://{s}.your-tile-provider.com/{z}/{x}/{y}.png?key=…',
attribution: '© Provider · © OpenStreetMap',
});
});
Developer notes
- OpenStreetMap's free tile server does not allow heavy production use; set your own provider with
tileUrland its credit withattribution. - With a single marker
centerandzoomstill apply; the marker'stitleshows as plain text in the popup that opens on click. - The map is drawn in
wire:ignore; later Livewire updates do not rebuild it.
Events
| Event | detail | Description |
|---|---|---|
map-picked | { lat, lng } | With pick, sent from the component when the map is clicked; it bubbles. Values are numbers. |