Skip to content

[Map] Add option to configure attribution and zoom control #2792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
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
7 changes: 7 additions & 0 deletions src/Map/src/Bridge/Leaflet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## 2.27

- Add `attributionControl` and `attributionControlOptions` to `LeafletOptions`,
to configure [attribution control](https://leafletjs.com/reference.html#map-attributioncontrol) and its options
- Add `zoomControl` and `zoomControlOptions` to `LeafletOptions`,
to configure [zoom control](https://leafletjs.com/reference.html#map-zoomcontrol) and its options

## 2.26

- Using `new LeafletOptions(tileLayer: false)` will now disable the default `TileLayer`.
Expand Down
7 changes: 7 additions & 0 deletions src/Map/src/Bridge/Leaflet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ You can use the `LeafletOptions` class to configure your `Map`::

```php
use Symfony\UX\Map\Bridge\Leaflet\LeafletOptions;
use Symfony\UX\Map\Bridge\Leaflet\Option\AttributionControlOptions;
use Symfony\UX\Map\Bridge\Leaflet\Option\ControlPosition;
use Symfony\UX\Map\Bridge\Leaflet\Option\TileLayer;
use Symfony\UX\Map\Bridge\Leaflet\Option\ZoomControlOptions;
use Symfony\UX\Map\Point;
use Symfony\UX\Map\Map;

Expand All @@ -50,6 +53,10 @@ $leafletOptions = (new LeafletOptions())
'maxZoom' => 10,
]
))
->attributionControl(false)
->attributionControlOptions(new AttributionControlOptions(ControlPosition::BOTTOM_LEFT))
->zoomControl(false)
->zoomControlOptions(new ZoomControlOptions(ControlPosition::TOP_LEFT))
;

// Add the custom options to the map
Expand Down
15 changes: 13 additions & 2 deletions src/Map/src/Bridge/Leaflet/assets/dist/map_controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ import AbstractMapController from '@symfony/ux-map';
import type { Icon, InfoWindowWithoutPositionDefinition, MarkerDefinition, Point, PolygonDefinition, PolylineDefinition } from '@symfony/ux-map';
import 'leaflet/dist/leaflet.min.css';
import * as L from 'leaflet';
import type { MapOptions as LeafletMapOptions, MarkerOptions, PolylineOptions as PolygonOptions, PolylineOptions, PopupOptions } from 'leaflet';
type MapOptions = Pick<LeafletMapOptions, 'center' | 'zoom'> & {
import type { ControlPosition, MapOptions as LeafletMapOptions, MarkerOptions, PolylineOptions as PolygonOptions, PolylineOptions, PopupOptions } from 'leaflet';
type MapOptions = Pick<LeafletMapOptions, 'center' | 'zoom' | 'attributionControl' | 'zoomControl'> & {
attributionControlOptions?: {
position: ControlPosition;
prefix: string | false;
};
zoomControlOptions?: {
position: ControlPosition;
zoomInText: string;
zoomInTitle: string;
zoomOutText: string;
zoomOutTitle: string;
};
tileLayer: {
url: string;
attribution: string;
Expand Down
8 changes: 8 additions & 0 deletions src/Map/src/Bridge/Leaflet/assets/dist/map_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,21 @@ class map_controller extends default_1 {
...options,
center: center === null ? undefined : center,
zoom: zoom === null ? undefined : zoom,
attributionControl: false,
zoomControl: false,
});
if (options.tileLayer) {
L.tileLayer(options.tileLayer.url, {
attribution: options.tileLayer.attribution,
...options.tileLayer.options,
}).addTo(map);
}
if (typeof options.attributionControlOptions !== 'undefined') {
L.control.attribution({ ...options.attributionControlOptions }).addTo(map);
}
if (typeof options.zoomControlOptions !== 'undefined') {
L.control.zoom({ ...options.zoomControlOptions }).addTo(map);
}
return map;
}
doCreateMarker({ definition }) {
Expand Down
21 changes: 20 additions & 1 deletion src/Map/src/Bridge/Leaflet/assets/src/map_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
import 'leaflet/dist/leaflet.min.css';
import * as L from 'leaflet';
import type {
ControlPosition,
LatLngBoundsExpression,
MapOptions as LeafletMapOptions,
MarkerOptions,
Expand All @@ -18,7 +19,15 @@ import type {
PopupOptions,
} from 'leaflet';

type MapOptions = Pick<LeafletMapOptions, 'center' | 'zoom'> & {
type MapOptions = Pick<LeafletMapOptions, 'center' | 'zoom' | 'attributionControl' | 'zoomControl'> & {
attributionControlOptions?: { position: ControlPosition; prefix: string | false };
zoomControlOptions?: {
position: ControlPosition;
zoomInText: string;
zoomInTitle: string;
zoomOutText: string;
zoomOutTitle: string;
};
tileLayer: { url: string; attribution: string; options: Record<string, unknown> } | false;
};

Expand Down Expand Up @@ -79,6 +88,8 @@ export default class extends AbstractMapController<
...options,
center: center === null ? undefined : center,
zoom: zoom === null ? undefined : zoom,
attributionControl: false,
zoomControl: false,
});

if (options.tileLayer) {
Expand All @@ -88,6 +99,14 @@ export default class extends AbstractMapController<
}).addTo(map);
}

if (typeof options.attributionControlOptions !== 'undefined') {
L.control.attribution({ ...options.attributionControlOptions }).addTo(map);
}

if (typeof options.zoomControlOptions !== 'undefined') {
L.control.zoom({ ...options.zoomControlOptions }).addTo(map);
}

return map;
}

Expand Down
68 changes: 64 additions & 4 deletions src/Map/src/Bridge/Leaflet/src/LeafletOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\UX\Map\Bridge\Leaflet;

use Symfony\UX\Map\Bridge\Leaflet\Option\AttributionControlOptions;
use Symfony\UX\Map\Bridge\Leaflet\Option\TileLayer;
use Symfony\UX\Map\Bridge\Leaflet\Option\ZoomControlOptions;
use Symfony\UX\Map\MapOptionsInterface;

/**
Expand All @@ -24,6 +26,10 @@ public function __construct(
url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
),
private bool $attributionControl = true,
private AttributionControlOptions $attributionControlOptions = new AttributionControlOptions(),
private bool $zoomControl = true,
private ZoomControlOptions $zoomControlOptions = new ZoomControlOptions(),
) {
}

Expand All @@ -34,23 +40,77 @@ public function tileLayer(TileLayer|false $tileLayer): self
return $this;
}

public function attributionControl(bool $enable = true): self
{
$this->attributionControl = $enable;

return $this;
}

public function attributionControlOptions(AttributionControlOptions $attributionControlOptions): self
{
$this->attributionControl = true;
$this->attributionControlOptions = $attributionControlOptions;

return $this;
}

public function zoomControl(bool $enable = true): self
{
$this->zoomControl = $enable;

return $this;
}

public function zoomControlOptions(ZoomControlOptions $zoomControlOptions): self
{
$this->zoomControl = true;
$this->zoomControlOptions = $zoomControlOptions;

return $this;
}

/**
* @internal
*/
public static function fromArray(array $array): MapOptionsInterface
{
return new self(
tileLayer: $array['tileLayer'] ? TileLayer::fromArray($array['tileLayer']) : false,
);
$array += ['attributionControl' => false, 'zoomControl' => false, 'tileLayer' => false];

if ($array['tileLayer']) {
$array['tileLayer'] = TileLayer::fromArray($array['tileLayer']);
}

if (isset($array['attributionControlOptions'])) {
$array['attributionControl'] = true;
$array['attributionControlOptions'] = AttributionControlOptions::fromArray($array['attributionControlOptions']);
}

if (isset($array['zoomControlOptions'])) {
$array['zoomControl'] = true;
$array['zoomControlOptions'] = ZoomControlOptions::fromArray($array['zoomControlOptions']);
}

return new self(...$array);
}

/**
* @internal
*/
public function toArray(): array
{
return [
$array = [
'tileLayer' => $this->tileLayer ? $this->tileLayer->toArray() : false,
];

if ($this->attributionControl) {
$array['attributionControlOptions'] = $this->attributionControlOptions->toArray();
}

if ($this->zoomControl) {
$array['zoomControlOptions'] = $this->zoomControlOptions->toArray();
}

return $array;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Map\Bridge\Leaflet\Option;

/**
* Options for the rendering of the attribution control.
*
* @see https://leafletjs.com/reference.html#control-zoom
*/
final class AttributionControlOptions
{
public function __construct(
private readonly ControlPosition $position = ControlPosition::BOTTOM_RIGHT,
private readonly string|false $prefix = 'Leaflet',
) {
}

/**
* @internal
*/
public static function fromArray(array $array): self
{
return new self(
position: ControlPosition::from($array['position']),
prefix: $array['prefix'],
);
}

/**
* @internal
*/
public function toArray(): array
{
return [
'position' => $this->position->value,
'prefix' => $this->prefix,
];
}
}
23 changes: 23 additions & 0 deletions src/Map/src/Bridge/Leaflet/src/Option/ControlPosition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Map\Bridge\Leaflet\Option;

/**
* @see https://leafletjs.com/reference.html#control-position
*/
enum ControlPosition: string
{
case TOP_LEFT = 'topleft';
case TOP_RIGHT = 'topright';
case BOTTOM_LEFT = 'bottomleft';
case BOTTOM_RIGHT = 'bottomright';
}
55 changes: 55 additions & 0 deletions src/Map/src/Bridge/Leaflet/src/Option/ZoomControlOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Map\Bridge\Leaflet\Option;

/**
* Options for the rendering of the zoom control.
*
* @see https://leafletjs.com/reference.html#control-zoom
*/
final class ZoomControlOptions
{
public function __construct(
private readonly ControlPosition $position = ControlPosition::TOP_LEFT,
private readonly string $zoomInText = '<span aria-hidden="true">+</span>',
private readonly string $zoomInTitle = 'Zoom in',
private readonly string $zoomOutText = '<span aria-hidden="true">&#x2212;</span>',
private readonly string $zoomOutTitle = 'Zoom out',
) {
}

/**
* @internal
*/
public static function fromArray(array $array): self
{
if (isset($array['position'])) {
$array['position'] = ControlPosition::from($array['position']);
}

return new self(...$array);
}

/**
* @internal
*/
public function toArray(): array
{
return [
'position' => $this->position->value,
'zoomInText' => $this->zoomInText,
'zoomInTitle' => $this->zoomInTitle,
'zoomOutText' => $this->zoomOutText,
'zoomOutTitle' => $this->zoomOutTitle,
];
}
}
Loading