-
-
Notifications
You must be signed in to change notification settings - Fork 356
[Map] Add support for libraries
for Google Bridge, inject provider's SDK (L
or google
) to dispatched events
#2044
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,19 +10,21 @@ UX_MAP_DSN=google://GOOGLE_MAPS_API_KEY@default | |
# With options | ||
UX_MAP_DSN=google://GOOGLE_MAPS_API_KEY@default?version=weekly | ||
UX_MAP_DSN=google://GOOGLE_MAPS_API_KEY@default?language=fr®ion=FR | ||
UX_MAP_DSN=google://GOOGLE_MAPS_API_KEY@default??libraries[]=geometry&libraries[]=places | ||
``` | ||
|
||
Available options: | ||
|
||
| Option | Description | Default | | ||
|------------|------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------| | ||
| `id` | The id of the script tag | `__googleMapsScriptId` | | ||
| `language` | Force language, see [list of supported languages](https://developers.google.com/maps/faq#languagesupport) specified in the browser | The user's preferred language | | ||
| `region` | Unicode region subtag identifiers compatible with [ISO 3166-1](https://en.wikipedia.org/wiki/ISO_3166-1) | | | ||
| `nonce` | Use a cryptographic nonce attribute | | | ||
| `retries` | The number of script load retries | 3 | | ||
| `url` | Custom url to load the Google Maps API script | `https://maps.googleapis.com/maps/api/js` | | ||
| `version` | The release channels or version numbers | `weekly` | | ||
| Option | Description | Default | | ||
|-------------|------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------| | ||
| `id` | The id of the script tag | `__googleMapsScriptId` | | ||
| `language` | Force language, see [list of supported languages](https://developers.google.com/maps/faq#languagesupport) specified in the browser | The user's preferred language | | ||
| `region` | Unicode region subtag identifiers compatible with [ISO 3166-1](https://en.wikipedia.org/wiki/ISO_3166-1) | | | ||
| `nonce` | Use a cryptographic nonce attribute | | | ||
| `retries` | The number of script load retries | 3 | | ||
| `url` | Custom url to load the Google Maps API script | `https://maps.googleapis.com/maps/api/js` | | ||
| `version` | The release channels or version numbers | `weekly` | | ||
| `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 | | ||
|
||
## Map options | ||
|
||
|
@@ -78,6 +80,60 @@ $googleOptions = (new GoogleOptions()) | |
// Add the custom options to the map | ||
$map->options($googleOptions); | ||
``` | ||
## Use cases | ||
|
||
Below are some common or advanced use cases when using a map. | ||
|
||
### Customize the marker | ||
|
||
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. | ||
|
||
Assuming you have a map with a custom controller: | ||
```twig | ||
{{ render_map(map, {'data-controller': 'my-map' }) }} | ||
``` | ||
|
||
You can create a Stimulus controller to customize the markers before they are created: | ||
```js | ||
// assets/controllers/my_map_controller.js | ||
import {Controller} from "@hotwired/stimulus"; | ||
|
||
export default class extends Controller | ||
{ | ||
connect() { | ||
this.element.addEventListener('ux:map:marker:before-create', this._onMarkerBeforeCreate); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is new: Or did i left way too long to remember ? (4 days hahaha) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always has been here ;) |
||
} | ||
|
||
disconnect() { | ||
// Always remove listeners when the controller is disconnected | ||
this.element.removeEventListener('ux:map:marker:before-create', this._onMarkerBeforeCreate); | ||
} | ||
|
||
_onMarkerBeforeCreate(event) { | ||
// You can access the marker definition and the google object | ||
// Note: `definition.rawOptions` is the raw options object that will be passed to the `google.maps.Marker` constructor. | ||
const { definition, google } = event.detail; | ||
|
||
// 1. To use a custom image for the marker | ||
const beachFlagImg = document.createElement("img"); | ||
// Note: instead of using an hardcoded URL, you can use the `extra` parameter from `new Marker()` (PHP) and access it here with `definition.extra`. | ||
beachFlagImg.src = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png"; | ||
Comment on lines
+118
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should illustrate this example with a controller "value" |
||
definition.rawOptions = { | ||
content: beachFlagImg | ||
} | ||
|
||
// 2. To use a custom glyph for the marker | ||
const pinElement = new google.maps.marker.PinElement({ | ||
// Note: instead of using an hardcoded URL, you can use the `extra` parameter from `new Marker()` (PHP) and access it here with `definition.extra`. | ||
glyph: new URL('https://maps.gstatic.com/mapfiles/place_api/icons/v2/museum_pinlet.svg'), | ||
glyphColor: "white", | ||
}); | ||
definition.rawOptions = { | ||
content: pinElement.element, | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Resources | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should that be in controller.json or something like that ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean?