[Map] Add fitBoundsToMarkers option to Twig extension and component

This commit is contained in:
Romain Monteil
2025-09-30 11:33:57 +02:00
committed by Hugo Alliaume
parent 8bef1f2024
commit 9928a5cfec
4 changed files with 22 additions and 2 deletions

View File

@@ -1,5 +1,9 @@
# CHANGELOG
## 2.31
- Add `fitBoundsToMarkers` parameter to `ux_map()` Twig function
## 2.30
- Ensure compatibility with PHP 8.5

View File

@@ -329,12 +329,17 @@ templates. The function accepts the same arguments as the ``Map`` class:
infoWindow: { content: 'Welcome to <b>New York</b>' }
},
],
fitBoundsToMarkers: true,
attributes: {
class: 'foo',
style: 'height: 800px; width: 100%; border: 4px solid red; margin-block: 10vh;',
}
) }}
.. versionadded:: 2.31
`fitBoundsToMarkers` option for the twig function is available since UX Map 2.31.
Twig Component ``<twig:ux:map />``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -354,6 +359,7 @@ Alternatively, you can use the ``<twig:ux:map />`` component.
"infoWindow": {"content": "Welcome to <b>New York</b>"}
}
]'
:fitBoundsToMarkers="true",
class="foo"
style="height: 800px; width: 100%; border: 4px solid red; margin-block: 10vh;"
/>
@@ -364,6 +370,10 @@ The ``<twig:ux:map />`` component requires the `Twig Component`_ package.
$ composer require symfony/ux-twig-component
.. versionadded:: 2.31
`fitBoundsToMarkers` option for the twig component is available since UX Map 2.31.
Interact with the map
~~~~~~~~~~~~~~~~~~~~~

View File

@@ -53,9 +53,10 @@ final class MapRuntime implements RuntimeExtensionInterface
?array $rectangles = null,
?float $minZoom = null,
?float $maxZoom = null,
?bool $fitBoundsToMarkers = null,
): string {
if ($map instanceof Map) {
if (null !== $center || null !== $zoom || $markers || $polygons || $polylines || $circles || $rectangles || $minZoom || $maxZoom) {
if (null !== $center || null !== $zoom || $markers || $polygons || $polylines || $circles || $rectangles || $minZoom || $maxZoom || null !== $fitBoundsToMarkers) {
throw new \InvalidArgumentException('It is not allowed to pass both a Map object and other parameters (like "center", "zoom", "markers", etc...) to the "renderMap" method. Please use either a Map object or the individual parameters.');
}
@@ -90,13 +91,16 @@ final class MapRuntime implements RuntimeExtensionInterface
if (null !== $maxZoom) {
$map->maxZoom($maxZoom);
}
if (null !== $fitBoundsToMarkers) {
$map->fitBoundsToMarkers($fitBoundsToMarkers);
}
return $this->renderer->renderMap($map, $attributes);
}
public function render(array $args = []): string
{
$map = array_intersect_key($args, array_flip(['map', 'center', 'zoom', 'markers', 'polygons', 'polylines', 'circles', 'rectangles', 'minZoom', 'maxZoom']));
$map = array_intersect_key($args, array_flip(['map', 'center', 'zoom', 'markers', 'polygons', 'polylines', 'circles', 'rectangles', 'minZoom', 'maxZoom', 'fitBoundsToMarkers']));
$attributes = array_diff_key($args, $map);
return $this->renderMap(...$map, attributes: $attributes);

View File

@@ -33,6 +33,8 @@ final class UXMapComponent
public ?Point $center;
public ?bool $fitBoundsToMarkers;
/**
* @var Marker[]
*/