You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR was squashed before being merged into the 2.x branch.
Discussion
----------
[Map] Allows Map options customization in `ux:map:pre-connect` event (e.g.: `zoom`, `options`, `bridgeOptions`...)
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes <!-- please update src/**/CHANGELOG.md files -->
| Docs? | yes <!-- required for new features -->
| Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License | MIT
<!--
Replace this notice by a description of your feature/bugfix.
This will help reviewers and should be a good start for the documentation.
Additionally (see https://symfony.com/releases):
- Always add tests and ensure they pass.
- For new features, provide some code snippets to help understand usage.
- Features and deprecations must be submitted against branch main.
- Update/add documentation as required (we can help!)
- Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
- Never break backward compatibility (see https://symfony.com/bc).
-->
This feature allows users to override the options passed to `L.Map()` or `new google.maps.Map()` trough the `ux:map:pre-connect` event.
The configurable options are:
- zoom
- center
- options
- bridgeOptions, not injected by default, but can be defined on-the-fly
This is a blocking step for #2810, but also for one of my need when I wanted to persist the zoom/center in the URL, to share my map (https://hugo.alliau.me/places 😛) :
```js
_onMapPreConnect = (event) => {
const { L } = event.detail;
if (window.location.hash) {
try {
const state = Object.fromEntries(new URLSearchParams(window.location.hash.slice(1)));
const zoom = Number(state.z);
const center = state.center.split(",").map(Number);
event.detail.zoom = zoom;
event.detail.center = L.latLng(center[0], center[1]);
} catch (e) {
console.error("Invalid state in URL hash:", e);
}
}
}
_onMapConnect = (event) => {
const { map } = event.detail;
const updateState = () => {
const center = map.getCenter();
const zoom = map.getZoom();
const state = {
z: zoom,
center: [center.lat.toFixed(5), center.lng.toFixed(5)],
};
window.history.replaceState(state, "", `#${new URLSearchParams(state).toString()}`);
};
map.addEventListener("zoom", () => updateState());
map.addEventListener("move", () => updateState());
};
```
Commits
-------
afc7db6 [Map] Add missing doc for Circle and Rectangle
b0463b7 [Map] Allows Map options customization in `ux:map:pre-connect` event (e.g.: `zoom`, `options`, `bridgeOptions`...)
* This event is triggered when the map is not created yet
383
391
* You can use this event to configure the map before it is created
384
392
*/
385
393
_onPreConnect(event) {
394
+
// You can read or write the zoom level
395
+
console.log(event.detail.zoom);
396
+
397
+
// You can read or write the center of the map
398
+
console.log(event.detail.center);
399
+
400
+
// You can read or write map options, specific to the Bridge, it represents the normalized `*Options` PHP class (e.g. `GoogleOptions`, `LeafletOptions`)
386
401
console.log(event.detail.options);
402
+
403
+
// Finally, you can also set Bridge-specific options that will be used when creating the map.
404
+
event.detail.bridgeOptions= {
405
+
preferCanvas:true, // e.g. for Leaflet (https://leafletjs.com/reference.html#map-prefercanvas)
406
+
backgroundColor:'#f0f0f0', // e.g. for Google Maps (https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions.backgroundColor)
407
+
}
387
408
}
388
409
389
410
/**
@@ -442,6 +463,10 @@ Symfony UX Map allows you to extend its default behavior using a custom Stimulus
442
463
console.log(event.detail.polygon);
443
464
// ... or a polyline
444
465
console.log(event.detail.polyline);
466
+
// ... or a circle
467
+
console.log(event.detail.circle);
468
+
// ... or a rectangle
469
+
console.log(event.detail.rectangle);
445
470
}
446
471
447
472
/**
@@ -479,6 +504,26 @@ Symfony UX Map allows you to extend its default behavior using a custom Stimulus
0 commit comments