diff --git a/src/Map/assets/dist/abstract_map_controller.d.ts b/src/Map/assets/dist/abstract_map_controller.d.ts index e7a257737be..07721103287 100644 --- a/src/Map/assets/dist/abstract_map_controller.d.ts +++ b/src/Map/assets/dist/abstract_map_controller.d.ts @@ -4,8 +4,8 @@ export type Point = { lng: number; }; export type MapView = { - center: Point; - zoom: number; + center: Point | null; + zoom: number | null; fitBoundsToMarkers: boolean; markers: Array>; options: Options; @@ -38,8 +38,8 @@ export default abstract class): Marker; diff --git a/src/Map/assets/src/abstract_map_controller.ts b/src/Map/assets/src/abstract_map_controller.ts index 633ce7e0e00..c97139b26f1 100644 --- a/src/Map/assets/src/abstract_map_controller.ts +++ b/src/Map/assets/src/abstract_map_controller.ts @@ -3,8 +3,8 @@ import { Controller } from '@hotwired/stimulus'; export type Point = { lat: number; lng: number }; export type MapView = { - center: Point; - zoom: number; + center: Point | null; + zoom: number | null; fitBoundsToMarkers: boolean; markers: Array>; options: Options; @@ -93,8 +93,8 @@ export default abstract class< zoom, options, }: { - center: Point; - zoom: number; + center: Point | null; + zoom: number | null; options: MapOptions; }): Map; diff --git a/src/Map/doc/index.rst b/src/Map/doc/index.rst index 78f63d1c16f..2e75e88cedb 100644 --- a/src/Map/doc/index.rst +++ b/src/Map/doc/index.rst @@ -78,8 +78,11 @@ A map is created by calling ``new Map()``. You can configure the center, zoom, a { // 1. Create a new map instance $myMap = (new Map()); + // Explicitly set the center and zoom ->center(new Point(46.903354, 1.888334)) ->zoom(6) + // Or automatically fit the bounds to the markers + ->fitBoundsToMarkers() ; // 2. You can add markers diff --git a/src/Map/src/Bridge/Google/assets/dist/map_controller.d.ts b/src/Map/src/Bridge/Google/assets/dist/map_controller.d.ts index 406671b9489..f3dc57d9057 100644 --- a/src/Map/src/Bridge/Google/assets/dist/map_controller.d.ts +++ b/src/Map/src/Bridge/Google/assets/dist/map_controller.d.ts @@ -10,8 +10,8 @@ export default class extends AbstractMapController; connect(): Promise; protected doCreateMap({ center, zoom, options, }: { - center: Point; - zoom: number; + center: Point | null; + zoom: number | null; options: MapOptions; }): google.maps.Map; protected doCreateMarker(definition: MarkerDefinition): google.maps.marker.AdvancedMarkerElement; diff --git a/src/Map/src/Bridge/Google/assets/src/map_controller.ts b/src/Map/src/Bridge/Google/assets/src/map_controller.ts index 2712903d36e..5728b6bb759 100644 --- a/src/Map/src/Bridge/Google/assets/src/map_controller.ts +++ b/src/Map/src/Bridge/Google/assets/src/map_controller.ts @@ -67,8 +67,8 @@ export default class extends AbstractMapController< zoom, options, }: { - center: Point; - zoom: number; + center: Point | null; + zoom: number | null; options: MapOptions; }): google.maps.Map { // We assume the following control options are enabled if their options are set diff --git a/src/Map/src/Bridge/Leaflet/assets/dist/map_controller.d.ts b/src/Map/src/Bridge/Leaflet/assets/dist/map_controller.d.ts index ce794285475..76bb1ef7a3c 100644 --- a/src/Map/src/Bridge/Leaflet/assets/dist/map_controller.d.ts +++ b/src/Map/src/Bridge/Leaflet/assets/dist/map_controller.d.ts @@ -12,9 +12,9 @@ type MapOptions = Pick & { }; export default class extends AbstractMapController { connect(): void; - protected doCreateMap({ center, zoom, options }: { - center: Point; - zoom: number; + protected doCreateMap({ center, zoom, options, }: { + center: Point | null; + zoom: number | null; options: MapOptions; }): LeafletMap; protected doCreateMarker(definition: MarkerDefinition): Marker; diff --git a/src/Map/src/Bridge/Leaflet/assets/dist/map_controller.js b/src/Map/src/Bridge/Leaflet/assets/dist/map_controller.js index 3e8d3b80de9..9ce14c9473e 100644 --- a/src/Map/src/Bridge/Leaflet/assets/dist/map_controller.js +++ b/src/Map/src/Bridge/Leaflet/assets/dist/map_controller.js @@ -13,11 +13,11 @@ class map_controller extends AbstractMapController { }); super.connect(); } - doCreateMap({ center, zoom, options }) { + doCreateMap({ center, zoom, options, }) { const map$1 = map(this.element, { ...options, - center, - zoom, + center: center === null ? undefined : center, + zoom: zoom === null ? undefined : zoom, }); tileLayer(options.tileLayer.url, { attribution: options.tileLayer.attribution, diff --git a/src/Map/src/Bridge/Leaflet/assets/src/map_controller.ts b/src/Map/src/Bridge/Leaflet/assets/src/map_controller.ts index 71230b141eb..eac5df9ae08 100644 --- a/src/Map/src/Bridge/Leaflet/assets/src/map_controller.ts +++ b/src/Map/src/Bridge/Leaflet/assets/src/map_controller.ts @@ -35,11 +35,15 @@ export default class extends AbstractMapController< super.connect(); } - protected doCreateMap({ center, zoom, options }: { center: Point; zoom: number; options: MapOptions }): LeafletMap { + protected doCreateMap({ + center, + zoom, + options, + }: { center: Point | null; zoom: number | null; options: MapOptions }): LeafletMap { const map = createMap(this.element, { ...options, - center, - zoom, + center: center === null ? undefined : center, + zoom: zoom === null ? undefined : zoom, }); createTileLayer(options.tileLayer.url, { diff --git a/src/Map/src/Map.php b/src/Map/src/Map.php index 77ad7656216..834e0d3d299 100644 --- a/src/Map/src/Map.php +++ b/src/Map/src/Map.php @@ -85,16 +85,18 @@ public function addMarker(Marker $marker): self public function toArray(): array { - if (null === $this->center) { - throw new InvalidArgumentException('The center of the map must be set.'); - } - - if (null === $this->zoom) { - throw new InvalidArgumentException('The zoom of the map must be set.'); + if (!$this->fitBoundsToMarkers) { + if (null === $this->center) { + throw new InvalidArgumentException('The map "center" must be explicitly set when not enabling "fitBoundsToMarkers" feature.'); + } + + if (null === $this->zoom) { + throw new InvalidArgumentException('The map "zoom" must be explicitly set when not enabling "fitBoundsToMarkers" feature.'); + } } return [ - 'center' => $this->center->toArray(), + 'center' => $this->center?->toArray(), 'zoom' => $this->zoom, 'fitBoundsToMarkers' => $this->fitBoundsToMarkers, 'options' => (object) ($this->options?->toArray() ?? []), diff --git a/src/Map/tests/MapTest.php b/src/Map/tests/MapTest.php index 293aa973806..c108e8f67c0 100644 --- a/src/Map/tests/MapTest.php +++ b/src/Map/tests/MapTest.php @@ -24,7 +24,7 @@ class MapTest extends TestCase public function testCenterValidation(): void { self::expectException(InvalidArgumentException::class); - self::expectExceptionMessage('The center of the map must be set.'); + self::expectExceptionMessage('The map "center" must be explicitly set when not enabling "fitBoundsToMarkers" feature.'); $map = new Map(); $map->toArray(); @@ -33,7 +33,7 @@ public function testCenterValidation(): void public function testZoomValidation(): void { self::expectException(InvalidArgumentException::class); - self::expectExceptionMessage('The zoom of the map must be set.'); + self::expectExceptionMessage('The map "zoom" must be explicitly set when not enabling "fitBoundsToMarkers" feature.'); $map = new Map( center: new Point(48.8566, 2.3522) @@ -41,6 +41,23 @@ public function testZoomValidation(): void $map->toArray(); } + public function testZoomAndCenterCanBeOmittedIfFitBoundsToMarkers(): void + { + $map = new Map( + fitBoundsToMarkers: true + ); + + $array = $map->toArray(); + + self::assertSame([ + 'center' => null, + 'zoom' => null, + 'fitBoundsToMarkers' => true, + 'options' => $array['options'], + 'markers' => [], + ], $array); + } + public function testWithMinimumConfiguration(): void { $map = new Map();