v0.0.2
Demos (demo panel, opens in a new tab)

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

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

Basic usage: view
BLADE

Set the centre with center ([latitude, longitude]) and the zoom with zoom; the default centre is Istanbul.

blade
<zb:map :center="[41.0082, 28.9784]" :zoom="14" :height="280" />
PropValues
center[latitude, longitude]
zoom1–19 (higher zooms in)
heightPixels (default 360)

Markers

Markers: view
BLADE

markers is an array of items with lat, lng and an optional title; with several markers the map fits them all.

blade
<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'],
]" />
PropValues
markersArray of ['lat', 'lng', 'title']

Picking a location

Picking a location: view
BLADE

With pick, a click puts a marker there and sends the map-picked event; click the map to try it.

blade
<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>
PropValues
pickLivewire property the location is written to
map-pickedEvent dispatched on click: { lat, lng }

With Livewire

BLADE

In Livewire the clicked location is written to the property you give pick, as ['lat' => …, 'lng' => …].

blade
{{-- 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
PropValues
pickName of the Livewire property

Props

The values the component takes. What are props and slots?

PropTypeDefaultDescription
center—[41.0082, 28.9784][latitude, longitude]
zoomnumber12Zoom level (up to 19)
markers—[]Array of ['lat', 'lng', 'title']; with more than one, the map is fitted to all of them
heightnumber360Map height (pixels)
pick—nullLivewire property the clicked location is written to as ['lat', 'lng']
registerMap: tileUrl—OpenStreetMapTile URL template
registerMap: attribution—© OpenStreetMapAttribution text

Installation

Install Leaflet and register it in resources/js/app.js with your own tile provider.

js
// 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

Events, hooks and details
  • OpenStreetMap's free tile server does not allow heavy production use; set your own provider with tileUrl and its credit with attribution.
  • With a single marker center and zoom still apply; the marker's title shows 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

EventdetailDescription
map-picked{ lat, lng }With pick, sent from the component when the map is clicked; it bubbles. Values are numbers.
Zubazar UI · ZubazarDesigned for people.