{"slug":"ux-map","title":"ux-map","summary":"Symfony UX Map: Google Maps and Leaflet maps built in PHP and rendered in Twig. Use for store locators, markers, popups, polygons, circles, fitting the map to markers, ux_map(), twig:ux:map, UX_MAP_DSN, map events and ComponentWithMapTrait.","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-12-02T16:48:38.574743Z","repo":{"url":"https://github.com/smnandre/symfony-ux-skills","stars":191,"forks":15,"license":"MIT","updatedAt":"2026-12-02T03:34:06Z"},"bodyHtml":"<hr>\n<h2>name: ux-map\ndescription: 'Symfony UX Map: Google Maps and Leaflet maps built in PHP and rendered in Twig. Use for store locators, markers, popups, polygons, circles, fitting the map to markers, ux_map(), twig:ux:map, UX_MAP_DSN, map events and ComponentWithMapTrait.'\nlicense: MIT\nmetadata:\nauthor: Simon André\nurl: <a href=\"https://smnandre.dev\">https://smnandre.dev</a></h2>\n<h1>UX Map</h1>\n<p>Interactive maps in Symfony with Leaflet or Google Maps. Build maps in PHP, render them in Twig, and extend them with Stimulus controllers. Supports markers, polygons, polylines, circles, rectangles, info windows, clustering, and LiveComponent integration.</p>\n<h2>Installation</h2>\n<pre><code># Install the base package\ncomposer require symfony/ux-map\n\n# Then install ONE renderer:\ncomposer require symfony/ux-leaflet-map    # Leaflet (free, open source)\n# OR\ncomposer require symfony/ux-google-map     # Google Maps (requires API key)\n</code></pre>\n<h2>Quick Reference</h2>\n<pre><code>Map                    PHP object, holds markers/shapes/options\nPoint                  latitude + longitude\nMarker                 pin on the map, optional InfoWindow\nPolygon                closed shape (array of Points)\nPolyline               open line (array of Points)\nCircle                 center Point + radius in meters\nRectangle              southWest + northEast Points\nInfoWindow             popup attached to a Marker or a shape\nux_map(map, attrs)     Twig function to render a Map\n&lt;twig:ux:map /&gt;        Twig component (requires ux-twig-component)\n</code></pre>\n<h2>Building a Map in PHP</h2>\n<pre><code>use Symfony\\UX\\Map\\Map;\nuse Symfony\\UX\\Map\\Point;\nuse Symfony\\UX\\Map\\Marker;\nuse Symfony\\UX\\Map\\InfoWindow;\nuse Symfony\\UX\\Map\\Polygon;\nuse Symfony\\UX\\Map\\Polyline;\nuse Symfony\\UX\\Map\\Circle;\nuse Symfony\\UX\\Map\\Icon\\Icon;\nuse Symfony\\UX\\Map\\Rectangle;\n\n$map = (new Map())\n    -&gt;center(new Point(48.8566, 2.3522))\n    -&gt;zoom(12)\n    -&gt;minZoom(3)\n    -&gt;maxZoom(18);\n\n// Marker with info window\n$map-&gt;addMarker(new Marker(\n    position: new Point(48.8566, 2.3522),\n    title: 'Paris',\n    infoWindow: new InfoWindow(\n        headerContent: '&lt;b&gt;Paris&lt;/b&gt;',\n        content: 'The capital of France',\n    ),\n));\n\n// Marker with custom icon (UX Icons integration)\n$map-&gt;addMarker(new Marker(\n    position: new Point(48.8738, 2.2950),\n    title: 'Eiffel Tower',\n    icon: Icon::ux('mdi:tower-eiffel')-&gt;width(32)-&gt;height(32),\n    extra: ['category' =&gt; 'landmark'],\n));\n\n// Polygon (closed area)\n$map-&gt;addPolygon(new Polygon(\n    points: [\n        new Point(48.8566, 2.3522),\n        new Point(48.8606, 2.3376),\n        new Point(48.8530, 2.3499),\n    ],\n    infoWindow: new InfoWindow(content: 'Central Paris area'),\n));\n\n// Polyline (open line)\n$map-&gt;addPolyline(new Polyline(\n    points: [\n        new Point(48.8566, 2.3522),\n        new Point(48.8738, 2.2950),\n    ],\n));\n\n// Circle (center + radius in meters)\n$map-&gt;addCircle(new Circle(\n    center: new Point(48.8566, 2.3522),\n    radius: 500,\n    infoWindow: new InfoWindow(content: '500m radius'),\n));\n\n// Rectangle (two corners)\n$map-&gt;addRectangle(new Rectangle(\n    southWest: new Point(48.8566, 2.3522),\n    northEast: new Point(50.6292, 3.0573),\n    infoWindow: new InfoWindow(content: 'Paris to Lille'),\n));\n\n// Auto-fit bounds to show all markers\n$map-&gt;fitBoundsToMarkers();\n</code></pre>\n<p>Every element has a <code>remove*()</code> counterpart (<code>removeMarker($marker)</code>, <code>removeRectangle($rectangle)</code>...), handy in a LiveComponent.</p>\n<h3>Clustering Many Points</h3>\n<p>Group nearby points server-side before adding markers:</p>\n<pre><code>use Symfony\\UX\\Map\\Cluster\\GridClusteringAlgorithm;   // or MortonClusteringAlgorithm\n\n$clusters = (new GridClusteringAlgorithm())-&gt;cluster($points, zoom: 5.0);\n\nforeach ($clusters as $cluster) {\n    $map-&gt;addMarker(new Marker(\n        position: $cluster-&gt;getCenter(),\n        title: \\sprintf('%d shops', $cluster-&gt;count()),\n    ));\n}\n</code></pre>\n<h2>Rendering in Twig</h2>\n<h3>Twig Function</h3>\n<pre><code>{# Basic rendering #}\n{{ ux_map(map, {style: 'height: 400px; width: 100%;'}) }}\n\n{# With custom attributes and Stimulus controller #}\n{{ ux_map(map, {\n    'data-controller': 'custom-map',\n    style: 'height: 400px;',\n    class: 'rounded shadow'\n}) }}\n</code></pre>\n<h3>Twig Component (HTML Syntax)</h3>\n<p>Requires <code>symfony/ux-twig-component</code>. Defines the map inline, in Twig:</p>\n<pre><code>&lt;twig:ux:map\n    :center=\"[48.8566, 2.3522]\"\n    zoom=\"12\"\n    :markers='[\n        {\"position\": [48.8566, 2.3522], \"title\": \"Paris\"},\n        {\"position\": [48.8738, 2.2950], \"title\": \"Eiffel Tower\",\n         \"infoWindow\": {\"content\": \"324m tall\"}}\n    ]'\n    :fitBoundsToMarkers=\"true\"\n    style=\"height: 400px; width: 100%;\"\n    class=\"rounded shadow\"\n/&gt;\n</code></pre>\n<h2>Configuration</h2>\n<pre><code># config/packages/ux_map.yaml\nux_map:\n    renderer: '%env(resolve:default::UX_MAP_DSN)%'\n\n    # Google Maps specific\n    google_maps:\n        default_map_id: null    # Optional: default Map ID for all maps\n</code></pre>\n<h3>Renderer DSN</h3>\n<p>Set in <code>.env</code>:</p>\n<pre><code># Leaflet (free)\nUX_MAP_DSN=leaflet://default\n\n# Google Maps (requires API key)\nUX_MAP_DSN=google://GOOGLE_MAPS_API_KEY@default\n\n# Google Maps with loader options\nUX_MAP_DSN=google://GOOGLE_MAPS_API_KEY@default?v=weekly&amp;language=fr&amp;region=FR\n</code></pre>\n<p>Google query options: <code>v</code> (API version or channel, default <code>weekly</code>), <code>language</code>, <code>region</code>, <code>libraries[]</code> (<code>maps</code> and <code>marker</code> always load), <code>authReferrerPolicy</code>, <code>mapIds[]</code>, <code>channel</code>, <code>solutionChannel</code>.</p>\n<p>If the map renders blank, check container height, the JavaScript entry point and the renderer setup in <a href=\"references/gotchas.md#map-not-showing\">map rendering troubleshooting</a>.</p>\n<h2>Renderer Options</h2>\n<h3>Leaflet Options</h3>\n<pre><code>use Symfony\\UX\\Map\\Bridge\\Leaflet\\LeafletOptions;\nuse Symfony\\UX\\Map\\Bridge\\Leaflet\\Option\\TileLayer;\nuse Symfony\\UX\\Map\\Bridge\\Leaflet\\Option\\ZoomControlOptions;\nuse Symfony\\UX\\Map\\Bridge\\Leaflet\\Option\\AttributionControlOptions;\nuse Symfony\\UX\\Map\\Bridge\\Leaflet\\Option\\ControlPosition;\n\n$leafletOptions = (new LeafletOptions())\n    -&gt;tileLayer(new TileLayer(\n        url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',\n        attribution: '&amp;copy; OpenStreetMap contributors',\n        options: ['minZoom' =&gt; 5, 'maxZoom' =&gt; 18],\n    ))\n    -&gt;zoomControl(true)\n    -&gt;zoomControlOptions(new ZoomControlOptions(ControlPosition::TOP_LEFT))\n    -&gt;attributionControl(true)\n    -&gt;attributionControlOptions(new AttributionControlOptions(ControlPosition::BOTTOM_LEFT));\n\n$map-&gt;options($leafletOptions);\n</code></pre>\n<h3>Google Maps Options</h3>\n<pre><code>use Symfony\\UX\\Map\\Bridge\\Google\\GoogleOptions;\nuse Symfony\\UX\\Map\\Bridge\\Google\\Option\\ControlPosition;\nuse Symfony\\UX\\Map\\Bridge\\Google\\Option\\GestureHandling;\nuse Symfony\\UX\\Map\\Bridge\\Google\\Option\\MapTypeControlOptions;\nuse Symfony\\UX\\Map\\Bridge\\Google\\Option\\MapTypeControlStyle;\nuse Symfony\\UX\\Map\\Bridge\\Google\\Option\\ZoomControlOptions;\nuse Symfony\\UX\\Map\\Bridge\\Google\\Option\\StreetViewControlOptions;\nuse Symfony\\UX\\Map\\Bridge\\Google\\Option\\FullscreenControlOptions;\n\n$googleOptions = (new GoogleOptions())\n    -&gt;mapId('YOUR_MAP_ID')\n    -&gt;gestureHandling(GestureHandling::GREEDY)\n    -&gt;backgroundColor('#f00')\n    -&gt;doubleClickZoom(true)\n    -&gt;zoomControlOptions(new ZoomControlOptions(\n        position: ControlPosition::BLOCK_START_INLINE_END,\n    ))\n    -&gt;mapTypeControlOptions(new MapTypeControlOptions(\n        mapTypeIds: ['roadmap'],\n        position: ControlPosition::INLINE_END_BLOCK_START,\n        style: MapTypeControlStyle::DROPDOWN_MENU,\n    ))\n    -&gt;streetViewControlOptions(new StreetViewControlOptions(\n        position: ControlPosition::BLOCK_END_INLINE_START,\n    ))\n    -&gt;fullscreenControlOptions(new FullscreenControlOptions(\n        position: ControlPosition::INLINE_START_BLOCK_END,\n    ));\n\n$map-&gt;options($googleOptions);\n</code></pre>\n<p>To disable controls:</p>\n<pre><code>$googleOptions = (new GoogleOptions())\n    -&gt;mapId('YOUR_MAP_ID')\n    -&gt;zoomControl(false)\n    -&gt;mapTypeControl(false)\n    -&gt;streetViewControl(false)\n    -&gt;fullscreenControl(false);\n</code></pre>\n<h2>Map Events (Stimulus)</h2>\n<p>Attach a custom Stimulus controller to interact with the underlying map:</p>\n<pre><code>// assets/controllers/custom-map_controller.js\nimport { Controller } from '@hotwired/stimulus';\n\nexport default class extends Controller {\n    connect() {\n        this.element.addEventListener('ux:map:connect', this._onConnect.bind(this));\n        this.element.addEventListener('ux:map:marker:after-create', this._onMarkerCreated.bind(this));\n    }\n\n    _onConnect(event) {\n        const { map, markers } = event.detail;\n        console.log('Map ready:', map);\n    }\n\n    _onMarkerCreated(event) {\n        const { marker, definition } = event.detail;\n        if (definition.extra?.category === 'landmark') {\n            console.log('Landmark marker created');\n        }\n    }\n}\n</code></pre>\n<pre><code>{{ ux_map(map, {'data-controller': 'custom-map', style: 'height: 400px;'}) }}\n</code></pre>\n<h3>Available Events</h3>\n<table>\n<thead>\n<tr>\n<th>Event</th>\n<th>Detail</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>ux:map:pre-connect</code></td>\n<td><code>{options, bridgeOptions, ...}</code></td>\n<td>Before the map is created; set <code>bridgeOptions</code> for native options</td>\n</tr>\n<tr>\n<td><code>ux:map:connect</code></td>\n<td><code>{map, markers, polygons, polylines, circles, rectangles, infoWindows, extra}</code></td>\n<td>Map is initialized and ready</td>\n</tr>\n<tr>\n<td><code>ux:map:marker:before-create</code></td>\n<td><code>{definition}</code></td>\n<td>Before a marker is added</td>\n</tr>\n<tr>\n<td><code>ux:map:marker:after-create</code></td>\n<td><code>{marker, definition}</code></td>\n<td>After a marker is added</td>\n</tr>\n<tr>\n<td><code>ux:map:polygon:before-create</code></td>\n<td><code>{definition}</code></td>\n<td>Before a polygon is added</td>\n</tr>\n<tr>\n<td><code>ux:map:polygon:after-create</code></td>\n<td><code>{polygon, definition}</code></td>\n<td>After a polygon is added</td>\n</tr>\n<tr>\n<td><code>ux:map:polyline:before-create</code></td>\n<td><code>{definition}</code></td>\n<td>Before a polyline is added</td>\n</tr>\n<tr>\n<td><code>ux:map:polyline:after-create</code></td>\n<td><code>{polyline, definition}</code></td>\n<td>After a polyline is added</td>\n</tr>\n<tr>\n<td><code>ux:map:circle:before-create</code></td>\n<td><code>{definition}</code></td>\n<td>Before a circle is added</td>\n</tr>\n<tr>\n<td><code>ux:map:circle:after-create</code></td>\n<td><code>{circle, definition}</code></td>\n<td>After a circle is added</td>\n</tr>\n<tr>\n<td><code>ux:map:rectangle:before-create</code></td>\n<td><code>{definition}</code></td>\n<td>Before a rectangle is added</td>\n</tr>\n<tr>\n<td><code>ux:map:rectangle:after-create</code></td>\n<td><code>{rectangle, definition}</code></td>\n<td>After a rectangle is added</td>\n</tr>\n<tr>\n<td><code>ux:map:info-window:before-create</code></td>\n<td><code>{definition}</code></td>\n<td>Before an info window is created</td>\n</tr>\n<tr>\n<td><code>ux:map:info-window:after-create</code></td>\n<td><code>{infoWindow, definition}</code></td>\n<td>After an info window is created</td>\n</tr>\n</tbody>\n</table>\n<h2>LiveComponent Integration</h2>\n<p>Use <code>ComponentWithMapTrait</code> to make the map reactive:</p>\n<pre><code>namespace App\\Twig\\Components;\n\nuse Symfony\\UX\\LiveComponent\\Attribute\\AsLiveComponent;\nuse Symfony\\UX\\LiveComponent\\Attribute\\LiveProp;\nuse Symfony\\UX\\LiveComponent\\DefaultActionTrait;\nuse Symfony\\UX\\Map\\InfoWindow;\nuse Symfony\\UX\\Map\\Live\\ComponentWithMapTrait;\nuse Symfony\\UX\\Map\\Map;\nuse Symfony\\UX\\Map\\Marker;\nuse Symfony\\UX\\Map\\Point;\n\n#[AsLiveComponent]\nfinal class MapSearch\n{\n    use DefaultActionTrait;\n    use ComponentWithMapTrait;\n\n    #[LiveProp(writable: true)]\n    public string $query = '';\n\n    protected function instantiateMap(): Map\n    {\n        $map = (new Map())\n            -&gt;center(new Point(48.8566, 2.3522))\n            -&gt;zoom(12)\n            -&gt;fitBoundsToMarkers();\n\n        foreach ($this-&gt;getFilteredLocations() as $location) {\n            $map-&gt;addMarker(new Marker(\n                position: new Point($location-&gt;lat, $location-&gt;lng),\n                title: $location-&gt;name,\n                infoWindow: new InfoWindow($location-&gt;name),\n            ));\n        }\n\n        return $map;\n    }\n\n    private function getFilteredLocations(): array\n    {\n        // Filter based on $this-&gt;query\n        return [];\n    }\n}\n</code></pre>\n<pre><code>{# templates/components/MapSearch.html.twig #}\n&lt;div {{ attributes }}&gt;\n    &lt;input type=\"search\" data-model=\"debounce(300)|query\" placeholder=\"Search locations...\"&gt;\n    {{ ux_map(this.map, {style: 'height: 400px;'}) }}\n&lt;/div&gt;\n</code></pre>\n<h2>Key Principles</h2>\n<p><strong>Build in PHP, render in Twig.</strong> The <code>Map</code> object is your data model. Add markers, polygons, circles in PHP (where you have access to your database/services). Render in Twig with a single function call.</p>\n<p><strong>The renderer is swappable.</strong> Leaflet and Google Maps are interchangeable. Your PHP code uses the same <code>Map</code>, <code>Marker</code>, <code>Point</code> classes regardless of renderer. Only renderer-specific options differ.</p>\n<p><strong>Use <code>extra</code> for custom data.</strong> The <code>extra</code> parameter on markers, polygons, and circles lets you attach arbitrary data that is available in JavaScript events.</p>\n<p><strong>Install a renderer.</strong> The base <code>symfony/ux-map</code> package provides the PHP API, but you must also install <code>symfony/ux-leaflet-map</code> (free) or <code>symfony/ux-google-map</code> (requires API key). The map renders once a renderer is installed.</p>\n<p><strong><code>Point(lat, lng)</code>: latitude first.</strong> This matches Google Maps convention but is the opposite of GeoJSON <code>[lng, lat]</code>. Getting the order wrong puts markers in the wrong place or in the ocean.</p>\n<p><strong><code>fitBoundsToMarkers()</code> is your friend.</strong> Instead of manually calculating center/zoom, let the map auto-fit to show all markers. If there are no markers, the map falls back to the <code>center</code> and <code>zoom</code> you set: always provide both as defaults.</p>\n<h2>References</h2>\n<ul>\n<li><a href=\"https://ux.symfony.com/map\">Symfony UX package page</a> (<a href=\"https://ux.symfony.com/map.md\">Markdown summary</a>) · <a href=\"https://packagist.org/packages/symfony/ux-map\"><code>symfony/ux-map</code> on Packagist</a>.</li>\n<li>Try the <a href=\"https://ux.symfony.com/map#demo\">interactive map demo</a>.</li>\n<li>For renderers, map features and browser integration, see the <a href=\"https://symfony.com/bundles/ux-map\">Symfony UX Map documentation</a>.</li>\n<li><strong>API</strong> (all classes, methods, options): <a href=\"references/api.md\">references/api.md</a></li>\n<li><strong>Patterns</strong> (common recipes, controllers, dynamic maps): <a href=\"references/patterns.md\">references/patterns.md</a></li>\n<li><strong>Gotchas</strong> (common mistakes, debugging): <a href=\"references/gotchas.md\">references/gotchas.md</a></li>\n</ul>\n<h2>See Also</h2>\n<ul>\n<li><strong>live-component</strong>: <code>ComponentWithMapTrait</code> for maps that follow LiveProps</li>\n<li><strong>stimulus</strong>: custom controllers listening to the <code>ux:map:*</code> events</li>\n</ul>\n","files":[{"path":"references/api.md","sizeBytes":10402,"isText":true},{"path":"references/gotchas.md","sizeBytes":6512,"isText":true},{"path":"references/patterns.md","sizeBytes":7386,"isText":true},{"path":"SKILL.md","sizeBytes":13718,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"notes-only","suspicious":0,"notes":4,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-12-02T16:49:27.329036Z","sha256":"8A639BC06AA08B3B662AA7F8AFE78F988396966711164EAC7733282EBCAC8310","sizeBytes":13582},"review":null,"source":{"repositoryUrl":"https://github.com/smnandre/symfony-ux-skills","path":"skills/ux-map","license":"MIT","commit":"78c25ed0b79bcc21d7611002dc81d8b87c3316e2","subtreeSha":"24636700E439A49F6BEF2D082ADBFA58069DE4AE3B5B01BF725C1117A7D8F890","lastSyncedAt":"2026-12-02T16:48:36.132977Z"},"reviewedAt":"2026-12-02T16:50:55.226517Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/smnandre/symfony-ux-skills/tree/main/skills/ux-map"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install smnandre-symfony-ux-skills@llmmart"},{"target":"git","command":"git clone https://github.com/smnandre/symfony-ux-skills.git"}]}