diff --git a/CHANGELOG.md b/CHANGELOG.md index ff1e1c6..a6a0ee6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/doc/index.rst b/doc/index.rst index 56afd33..f704d1c 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -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 ------------ diff --git a/src/Elements.php b/src/Elements.php index c68876c..f8c75ce 100644 --- a/src/Elements.php +++ b/src/Elements.php @@ -64,6 +64,13 @@ abstract class Elements return $this; } + public function removeAll(): static + { + $this->elements->removeAll($this->elements); + + return $this; + } + public function toArray(): array { foreach ($this->elements as $element) { diff --git a/src/Map.php b/src/Map.php index 0df52df..369d43f 100644 --- a/src/Map.php +++ b/src/Map.php @@ -133,6 +133,13 @@ final class Map return $this; } + public function removeAllMarkers(): self + { + $this->markers->removeAll(); + + return $this; + } + public function addPolygon(Polygon $polygon): self { $this->polygons->add($polygon); @@ -147,6 +154,13 @@ final class Map return $this; } + public function removeAllPolygons(): self + { + $this->polygons->removeAll(); + + return $this; + } + public function addPolyline(Polyline $polyline): self { $this->polylines->add($polyline); @@ -161,6 +175,13 @@ final class Map return $this; } + public function removeAllPolylines(): self + { + $this->polylines->removeAll(); + + return $this; + } + public function addCircle(Circle $circle): self { $this->circles->add($circle); @@ -175,6 +196,13 @@ final class Map return $this; } + public function removeAllCircles(): self + { + $this->circles->removeAll(); + + return $this; + } + public function addRectangle(Rectangle $rectangle): self { $this->rectangles->add($rectangle); @@ -189,6 +217,13 @@ final class Map return $this; } + public function removeAllRectangles(): self + { + $this->rectangles->removeAll(); + + return $this; + } + /** * @param array $extra Extra data forwarded to the JavaScript side. It can be used in your custom * Stimulus controller to benefit from greater flexibility and customization.