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
feature #2044 [Map] Add support for libraries for Google Bridge, inject provider's SDK (L or google) to dispatched events (Kocal)
This PR was squashed before being merged into the 2.x branch.
Discussion
----------
[Map] Add support for `libraries` for Google Bridge, inject provider's SDK (`L` or `google`) to dispatched events
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes <!-- please update src/**/CHANGELOG.md files -->
| 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.
- 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).
-->
Follows #2040.
This PR gives the developper access to `L` (if using Leaflet) or `google` (if using Google Maps) in dispatched events, so the developper can **fully and freely** customize the map, their markers (before and after creation), and info windows (before and after creation).
I've added some use cases/examples in respective documentation, but tell me if a better place fits!
Also, please no quick merge this time (even if I like that :D), I really wants some reviews for this PR! cc `@javiereguiluz` 🙏🏻
# Code and screenshots
On my personal project, I have a map with a lot of markers.
Before UX Map, I was using Google Maps because I've found the "glyph" feature really sexy, but I was not able to use it anymore... until now!
## With Google Maps
Code:
```js
import {Controller} from "`@hotwired`/stimulus";
export default class extends Controller
{
connect() {
this.element.addEventListener('ux:map:marker:before-create', this._onMarkerBeforeCreate);
}
disconnect() {
this.element.removeEventListener('ux:map:marker:before-create', this._onMarkerBeforeCreate);
}
_onMarkerBeforeCreate(event) {
const { definition, google } = event.detail;
const pinElement = new google.maps.marker.PinElement({
glyph: new URL(String(definition.extra['icon_mask_uri'])), // I've filled `extra` parameter from `new Marker()` (PHP) with the icon mask URL
glyphColor: "white",
});
definition.rawOptions = {
content: pinElement.element,
}
}
}
```
Screenshot:
<img width="470" alt="Capture d’écran 2024-08-09 à 22 58 19" src="https://github.com/user-attachments/assets/ace5a033-a423-45c5-bd67-0da68dd188c1">
## With Leaflet
A dumb example taken from the website:
Code:
```js
import {Controller} from "`@hotwired`/stimulus";
export default class extends Controller
{
connect() {
this.element.addEventListener('ux:map:marker:before-create', this._onMarkerBeforeCreate);
}
disconnect() {
this.element.removeEventListener('ux:map:marker:before-create', this._onMarkerBeforeCreate);
}
_onMarkerBeforeCreate(event) {
const { definition, L } = event.detail;
const redIcon = L.icon({
iconUrl: 'https://leafletjs.com/examples/custom-icons/leaf-red.png',
shadowUrl: 'https://leafletjs.com/examples/custom-icons/leaf-shadow.png',
iconSize: [38, 95], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
})
definition.rawOptions = {
icon: redIcon,
}
}
}
```
Screenshot:
<img width="495" alt="Capture d’écran 2024-08-09 à 23 19 23" src="https://github.com/user-attachments/assets/e771c133-794a-4693-bfbb-5a3118f5f7f5">
Commits
-------
2dbb169 [Map] Add support for `libraries` for Google Bridge, inject provider's SDK (`L` or `google`) to dispatched events
|`id`| The id of the script tag |`__googleMapsScriptId`|
20
-
|`language`| Force language, see [list of supported languages](https://developers.google.com/maps/faq#languagesupport) specified in the browser | The user's preferred language |
21
-
|`region`| Unicode region subtag identifiers compatible with [ISO 3166-1](https://en.wikipedia.org/wiki/ISO_3166-1)||
22
-
|`nonce`| Use a cryptographic nonce attribute ||
23
-
|`retries`| The number of script load retries | 3 |
24
-
|`url`| Custom url to load the Google Maps API script |`https://maps.googleapis.com/maps/api/js`|
25
-
|`version`| The release channels or version numbers |`weekly`|
|`id`| The id of the script tag |`__googleMapsScriptId`|
21
+
|`language`| Force language, see [list of supported languages](https://developers.google.com/maps/faq#languagesupport) specified in the browser | The user's preferred language |
22
+
|`region`| Unicode region subtag identifiers compatible with [ISO 3166-1](https://en.wikipedia.org/wiki/ISO_3166-1)||
23
+
|`nonce`| Use a cryptographic nonce attribute ||
24
+
|`retries`| The number of script load retries | 3 |
25
+
|`url`| Custom url to load the Google Maps API script |`https://maps.googleapis.com/maps/api/js`|
26
+
|`version`| The release channels or version numbers |`weekly`|
27
+
|`libraries`| The additional libraries to load, see [list of supported libraries](https://googlemaps.github.io/js-api-loader/types/Library.html)|`['maps', 'marker']`, those two libraries are always loaded |
26
28
27
29
## Map options
28
30
@@ -78,6 +80,60 @@ $googleOptions = (new GoogleOptions())
78
80
// Add the custom options to the map
79
81
$map->options($googleOptions);
80
82
```
83
+
## Use cases
84
+
85
+
Below are some common or advanced use cases when using a map.
86
+
87
+
### Customize the marker
88
+
89
+
A common use case is to customize the marker. You can listen to the `ux:map:marker:before-create` event to customize the marker before it is created.
0 commit comments