Skip to content

Commit 3acffb5

Browse files
committed
[Map] Add the possibility to not configure map zoom/center if fit bounds to markers
1 parent 21595a7 commit 3acffb5

File tree

8 files changed

+46
-24
lines changed

8 files changed

+46
-24
lines changed

src/Map/assets/dist/abstract_map_controller.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ export type Point = {
44
lng: number;
55
};
66
export type MapView<Options, MarkerOptions, InfoWindowOptions> = {
7-
center: Point;
8-
zoom: number;
7+
center: Point | null;
8+
zoom: number | null;
99
fitBoundsToMarkers: boolean;
1010
markers: Array<MarkerDefinition<MarkerOptions, InfoWindowOptions>>;
1111
options: Options;
@@ -38,8 +38,8 @@ export default abstract class<MapOptions, Map, MarkerOptions, Marker, InfoWindow
3838
initialize(): void;
3939
connect(): void;
4040
protected abstract doCreateMap({ center, zoom, options, }: {
41-
center: Point;
42-
zoom: number;
41+
center: Point | null;
42+
zoom: number | null;
4343
options: MapOptions;
4444
}): Map;
4545
createMarker(definition: MarkerDefinition<MarkerOptions, InfoWindowOptions>): Marker;

src/Map/assets/src/abstract_map_controller.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Controller } from '@hotwired/stimulus';
33
export type Point = { lat: number; lng: number };
44

55
export type MapView<Options, MarkerOptions, InfoWindowOptions> = {
6-
center: Point;
7-
zoom: number;
6+
center: Point|null;
7+
zoom: number|null;
88
fitBoundsToMarkers: boolean;
99
markers: Array<MarkerDefinition<MarkerOptions, InfoWindowOptions>>;
1010
options: Options;
@@ -93,8 +93,8 @@ export default abstract class<
9393
zoom,
9494
options,
9595
}: {
96-
center: Point;
97-
zoom: number;
96+
center: Point|null;
97+
zoom: number|null;
9898
options: MapOptions;
9999
}): Map;
100100

src/Map/doc/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ A map is created by calling ``new Map()``. You can configure the center, zoom, a
7878
{
7979
// 1. Create a new map instance
8080
$myMap = (new Map());
81+
// Explicitly set the center and zoom
8182
->center(new Point(46.903354, 1.888334))
8283
->zoom(6)
84+
// Or automatically fit the bounds to the markers
85+
->fitBoundsToMarkers()
8386
;
8487
8588
// 2. You can add markers

src/Map/src/Bridge/Leaflet/assets/dist/map_controller.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type MapOptions = Pick<LeafletMapOptions, 'center' | 'zoom'> & {
1313
export default class extends AbstractMapController<MapOptions, typeof LeafletMap, MarkerOptions, Marker, Popup, PopupOptions> {
1414
connect(): void;
1515
protected doCreateMap({ center, zoom, options }: {
16-
center: Point;
17-
zoom: number;
16+
center: Point | null;
17+
zoom: number | null;
1818
options: MapOptions;
1919
}): LeafletMap;
2020
protected doCreateMarker(definition: MarkerDefinition): Marker;

src/Map/src/Bridge/Leaflet/assets/dist/map_controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class map_controller extends AbstractMapController {
1616
doCreateMap({ center, zoom, options }) {
1717
const map$1 = map(this.element, {
1818
...options,
19-
center,
20-
zoom,
19+
center: center === null ? undefined : center,
20+
zoom: zoom === null ? undefined : zoom,
2121
});
2222
tileLayer(options.tileLayer.url, {
2323
attribution: options.tileLayer.attribution,

src/Map/src/Bridge/Leaflet/assets/src/map_controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ export default class extends AbstractMapController<
3535
super.connect();
3636
}
3737

38-
protected doCreateMap({ center, zoom, options }: { center: Point; zoom: number; options: MapOptions }): LeafletMap {
38+
protected doCreateMap({ center, zoom, options }: { center: Point|null; zoom: number|null; options: MapOptions }): LeafletMap {
3939
const map = createMap(this.element, {
4040
...options,
41-
center,
42-
zoom,
41+
center: center === null ? undefined : center,
42+
zoom: zoom === null ? undefined : zoom,
4343
});
4444

4545
createTileLayer(options.tileLayer.url, {

src/Map/src/Map.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,18 @@ public function addMarker(Marker $marker): self
8585

8686
public function toArray(): array
8787
{
88-
if (null === $this->center) {
89-
throw new InvalidArgumentException('The center of the map must be set.');
90-
}
91-
92-
if (null === $this->zoom) {
93-
throw new InvalidArgumentException('The zoom of the map must be set.');
88+
if (!$this->fitBoundsToMarkers) {
89+
if (null === $this->center) {
90+
throw new InvalidArgumentException('The map "center" must be explicitly set when not enabling "fitBoundsToMarkers" feature.');
91+
}
92+
93+
if (null === $this->zoom) {
94+
throw new InvalidArgumentException('The map "zoom" must be explicitly set when not enabling "fitBoundsToMarkers" feature.');
95+
}
9496
}
9597

9698
return [
97-
'center' => $this->center->toArray(),
99+
'center' => $this->center?->toArray(),
98100
'zoom' => $this->zoom,
99101
'fitBoundsToMarkers' => $this->fitBoundsToMarkers,
100102
'options' => (object) ($this->options?->toArray() ?? []),

src/Map/tests/MapTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MapTest extends TestCase
2424
public function testCenterValidation(): void
2525
{
2626
self::expectException(InvalidArgumentException::class);
27-
self::expectExceptionMessage('The center of the map must be set.');
27+
self::expectExceptionMessage('The map "center" must be explicitly set when not enabling "fitBoundsToMarkers" feature.');
2828

2929
$map = new Map();
3030
$map->toArray();
@@ -33,14 +33,31 @@ public function testCenterValidation(): void
3333
public function testZoomValidation(): void
3434
{
3535
self::expectException(InvalidArgumentException::class);
36-
self::expectExceptionMessage('The zoom of the map must be set.');
36+
self::expectExceptionMessage('The map "zoom" must be explicitly set when not enabling "fitBoundsToMarkers" feature.');
3737

3838
$map = new Map(
3939
center: new Point(48.8566, 2.3522)
4040
);
4141
$map->toArray();
4242
}
4343

44+
public function testZoomAndCenterCanBeOmittedIfFitBoundsToMarkers(): void
45+
{
46+
$map = new Map(
47+
fitBoundsToMarkers: true
48+
);
49+
50+
$array = $map->toArray();
51+
52+
self::assertSame([
53+
'center' => null,
54+
'zoom' => null,
55+
'fitBoundsToMarkers' => true,
56+
'options' => $array['options'],
57+
'markers' => [],
58+
], $array);
59+
}
60+
4461
public function testWithMinimumConfiguration(): void
4562
{
4663
$map = new Map();

0 commit comments

Comments
 (0)