Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Map/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.32

- Add `Map::removeAllMarkers()`, `Map::removeAllPolygons()`, `Map::removeAllPolylines()`, `Map::removeAllCircles()` and `Map::removeAllRectangles()` methods

## 2.31

- Add `fitBoundsToMarkers` parameter to `ux_map()` Twig function
Expand Down
16 changes: 16 additions & 0 deletions src/Map/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,22 @@ If you haven't stored the element instance, you can still remove them by passing
$map->removeCircle('my-circle');
$map->removeRectangle('my-rectangle');

To remove all instances of a certain element, you can use the `Map::removeAll*()` methods::

// Add elements
$map->addMarker($marker = new Marker(/* ... */));
$map->addPolygon($polygon = new Polygon(/* ... */));
$map->addPolyline($polyline = new Polyline(/* ... */));
$map->addCircle($circle = new Circle(/* ... */));
$map->addRectangle($rectangle = new Rectangle(/* ... */));

// And later, remove those elements
$map->removeAllMarkers();
$map->removeAllPolygons();
$map->removeAllPolylines();
$map->removeAllCircles();
$map->removeAllRectangles();

Render a map
------------

Expand Down
50 changes: 50 additions & 0 deletions src/Map/src/Bridge/Google/tests/GoogleRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ public static function provideTestRenderMap(): iterable
->removeMarker('marker2'),
];

yield 'with all markers removed' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addMarker($marker1)
->addMarker($marker2)
->removeAllMarkers(),
];

yield 'with marker remove and new ones added' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
Expand All @@ -94,6 +104,16 @@ public static function provideTestRenderMap(): iterable
->addPolygon(new Polygon(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon'))),
];

yield 'with all polygons removed' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addPolygon(new Polygon(points: [new Point(48.8566, 2.3522), new Point(48.8566, 2.3522), new Point(48.8566, 2.3522)]))
->addPolygon(new Polygon(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon')))
->removeAllPolygons(),
];

yield 'with polylines and infoWindows' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
Expand All @@ -103,6 +123,16 @@ public static function provideTestRenderMap(): iterable
->addPolyline(new Polyline(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon'))),
];

yield 'with all polylines removed' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addPolyline(new Polyline(points: [new Point(48.8566, 2.3522), new Point(48.8566, 2.3522), new Point(48.8566, 2.3522)]))
->addPolyline(new Polyline(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polyline')))
->removeAllPolylines(),
];

yield 'with circles and infoWindows' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
Expand All @@ -112,6 +142,16 @@ public static function provideTestRenderMap(): iterable
->addCircle(new Circle(center: new Point(1.1, 2.2), radius: 1000, infoWindow: new InfoWindow(content: 'Circle'))),
];

yield 'with all circles removed' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addCircle(new Circle(center: new Point(48.8566, 2.3522), radius: 500, infoWindow: new InfoWindow(content: 'Circle')))
->addCircle(new Circle(center: new Point(1.1, 2.2), radius: 1000, infoWindow: new InfoWindow(content: 'Circle')))
->removeAllCircles(),
];

yield 'with rectangles and infoWindows' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
Expand All @@ -121,6 +161,16 @@ public static function provideTestRenderMap(): iterable
->addRectangle(new Rectangle(southWest: new Point(1.1, 2.2), northEast: new Point(3.3, 4.4), infoWindow: new InfoWindow(content: 'Rectangle'))),
];

yield 'with all rectangles removed' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addRectangle(new Rectangle(southWest: new Point(48.8566, 2.3522), northEast: new Point(48.8566, 2.3522), infoWindow: new InfoWindow(content: 'Rectangle')))
->addRectangle(new Rectangle(southWest: new Point(1.1, 2.2), northEast: new Point(3.3, 4.4), infoWindow: new InfoWindow(content: 'Rectangle')))
->removeAllRectangles(),
];

yield 'with controls enabled' => [
'renderer' => new GoogleRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
Expand Down
50 changes: 50 additions & 0 deletions src/Map/src/Bridge/Leaflet/tests/LeafletRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public static function provideTestRenderMap(): iterable
->removeMarker('marker2'),
];

yield 'with all markers removed' => [
'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addMarker($marker1)
->addMarker($marker2)
->removeAllMarkers(),
];

yield 'with marker remove and new ones added' => [
'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)),
'map' => (new Map())
Expand All @@ -89,6 +99,16 @@ public static function provideTestRenderMap(): iterable
->addPolygon(new Polygon(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon'), id: 'polygon2')),
];

yield 'with all polygons removed' => [
'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addPolygon(new Polygon(points: [new Point(48.8566, 2.3522), new Point(48.8566, 2.3522), new Point(48.8566, 2.3522)]))
->addPolygon(new Polygon(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polygon')))
->removeAllPolygons(),
];

yield 'with polylines and infoWindows' => [
'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)),
'map' => (new Map())
Expand All @@ -98,6 +118,16 @@ public static function provideTestRenderMap(): iterable
->addPolyline(new Polyline(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polyline'), id: 'polyline2')),
];

yield 'with all polylines removed' => [
'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addPolyline(new Polyline(points: [new Point(48.8566, 2.3522), new Point(48.8566, 2.3522), new Point(48.8566, 2.3522)]))
->addPolyline(new Polyline(points: [new Point(1.1, 2.2), new Point(3.3, 4.4), new Point(5.5, 6.6)], infoWindow: new InfoWindow(content: 'Polyline')))
->removeAllPolylines(),
];

yield 'with circles and infoWindows' => [
'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)),
'map' => (new Map())
Expand All @@ -107,6 +137,16 @@ public static function provideTestRenderMap(): iterable
->addCircle(new Circle(center: new Point(1.1, 2.2), radius: 500, infoWindow: new InfoWindow(content: 'Circle'), id: 'circle2')),
];

yield 'with all circles removed' => [
'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addCircle(new Circle(center: new Point(48.8566, 2.3522), radius: 500, infoWindow: new InfoWindow(content: 'Circle')))
->addCircle(new Circle(center: new Point(1.1, 2.2), radius: 1000, infoWindow: new InfoWindow(content: 'Circle')))
->removeAllCircles(),
];

yield 'with rectangles and infoWindows' => [
'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null)),
'map' => (new Map())
Expand All @@ -125,6 +165,16 @@ public static function provideTestRenderMap(): iterable
)),
];

yield 'with all rectangles removed' => [
'renderer' => new LeafletRenderer(new StimulusHelper(null), new UxIconRenderer(null), apiKey: 'api_key'),
'map' => (new Map())
->center(new Point(48.8566, 2.3522))
->zoom(12)
->addRectangle(new Rectangle(southWest: new Point(48.8566, 2.3522), northEast: new Point(48.8566, 2.3522), infoWindow: new InfoWindow(content: 'Rectangle')))
->addRectangle(new Rectangle(southWest: new Point(1.1, 2.2), northEast: new Point(3.3, 4.4), infoWindow: new InfoWindow(content: 'Rectangle')))
->removeAllRectangles(),
];

yield 'markers with icons' => [
'renderer' => new LeafletRenderer(
new StimulusHelper(null),
Expand Down
7 changes: 7 additions & 0 deletions src/Map/src/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public function remove(Element|string $elementOrId): static
return $this;
}

public function removeAll(): static
{
$this->elements->removeAll($this->elements);

return $this;
}

public function toArray(): array
{
foreach ($this->elements as $element) {
Expand Down
35 changes: 35 additions & 0 deletions src/Map/src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ public function removeMarker(Marker|string $markerOrId): self
return $this;
}

public function removeAllMarkers(): self
{
$this->markers->removeAll();

return $this;
}

public function addPolygon(Polygon $polygon): self
{
$this->polygons->add($polygon);
Expand All @@ -147,6 +154,13 @@ public function removePolygon(Polygon|string $polygonOrId): self
return $this;
}

public function removeAllPolygons(): self
{
$this->polygons->removeAll();

return $this;
}

public function addPolyline(Polyline $polyline): self
{
$this->polylines->add($polyline);
Expand All @@ -161,6 +175,13 @@ public function removePolyline(Polyline|string $polylineOrId): self
return $this;
}

public function removeAllPolylines(): self
{
$this->polylines->removeAll();

return $this;
}

public function addCircle(Circle $circle): self
{
$this->circles->add($circle);
Expand All @@ -175,6 +196,13 @@ public function removeCircle(Circle|string $circleOrId): self
return $this;
}

public function removeAllCircles(): self
{
$this->circles->removeAll();

return $this;
}

public function addRectangle(Rectangle $rectangle): self
{
$this->rectangles->add($rectangle);
Expand All @@ -189,6 +217,13 @@ public function removeRectangle(Rectangle|string $rectangleOrId): self
return $this;
}

public function removeAllRectangles(): self
{
$this->rectangles->removeAll();

return $this;
}

/**
* @param array<string, mixed> $extra Extra data forwarded to the JavaScript side. It can be used in your custom
* Stimulus controller to benefit from greater flexibility and customization.
Expand Down
Loading