-
Notifications
You must be signed in to change notification settings - Fork 0
feat(symbol): Allow to set custom symbol #74
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /** | ||
| * Copyright COORDONNÉES 2025, 2026 | ||
| * SPDX-License-Identifier: MPL-2.0 | ||
| */ | ||
|
|
||
| import type { Map as MapLibreMap } from "maplibre-gl"; | ||
|
|
||
| export function getLayerSymbolId({ layerId }: { layerId: string }) { | ||
| return `${layerId}-symbol-layer`; | ||
| } | ||
|
|
||
| function getLayerSymbolPictureId({ layerId }: { layerId: string }) { | ||
| return `${layerId}-symbol-picture-identifier`; | ||
| } | ||
|
|
||
| export function makeSetLayerSymbol({ map }: { map: MapLibreMap }) { | ||
| /** | ||
| * Update the icon-image of a Layer Layout. | ||
| * To use an image from your sprite, use spriteId. | ||
| * To use an external image, use symbolUrl. | ||
| * To use an svg image, use svg. | ||
| * Use only one provider. You can provide an additional fallback image via fallbackId. | ||
| * @param params.layerId — The ID of the layer to set the layout property in. | ||
| * @param params.imageUrl — The URL of the image file. Image file must be in png, webp, or jpg format. | ||
| * @param params.spriteId — The ID of the image to load from the sprite attached to the map instance. | ||
| * @param params.iconSize — The units in factor of the original icon size | ||
| * @param params.svg — The SVG to use as picture. | ||
| * @param params.fallbackId — The ID of a picture (from a sprite or from addImage) | ||
| * to use as fallback when the main image couldn't load. | ||
| */ | ||
| async function setLayerSymbol({ | ||
| layerId, | ||
| iconSize = 1, | ||
| fallbackId, | ||
| imageUrl, | ||
| spriteId, | ||
| svg, | ||
| }: { | ||
| layerId: string; | ||
| iconSize?: number; | ||
| fallbackId?: string; | ||
| } & ( | ||
| | { imageUrl: string; spriteId?: null; svg?: null } | ||
| | { spriteId: string; imageUrl?: null; svg?: null } | ||
| | { svg: string; imageUrl?: null; spriteId?: null } | ||
| )) { | ||
| function setLayerIconImage(pictureId: string) { | ||
| const finalId = fallbackId | ||
| ? ["coalesce", ["image", pictureId], ["image", fallbackId]] | ||
| : pictureId; | ||
| map.setLayoutProperty(layerId, "icon-image", finalId); | ||
| map.setLayoutProperty(layerId, "icon-size", iconSize); | ||
| } | ||
|
|
||
| if (spriteId) { | ||
| setLayerIconImage(spriteId); | ||
| return; | ||
| } | ||
|
|
||
| if (imageUrl) { | ||
| const image = await map.loadImage(imageUrl); | ||
| const imageId = getLayerSymbolPictureId({ layerId }); | ||
| map.addImage(imageId, image.data); | ||
|
|
||
| setLayerIconImage(imageId); | ||
| return; | ||
| } | ||
|
|
||
| if (svg) { | ||
| // Ref: https://maplibre.org/maplibre-gl-js/docs/examples/display-a-remote-svg-symbol/ | ||
| const image = new Image(); | ||
| const promise = new Promise((resolve) => { | ||
| image.onload = resolve; | ||
| }); | ||
| image.src = svg; | ||
| await promise; // Wait for the image to load | ||
| const imageId = getLayerSymbolPictureId({ layerId }); | ||
| map.addImage(imageId, image); | ||
|
|
||
| setLayerIconImage(imageId); | ||
| return; | ||
| } | ||
|
|
||
| console.warn("[setLayerSymbol] Provide one of: spriteId, imageUrl, svg"); | ||
| } | ||
|
|
||
| return setLayerSymbol; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 happens if both imageUrl and svg are set ?
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.
I would have said "no worries, ts does not allow that" but not all consumers projects will be in typescript 😮💨 I'll add constraints
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.
Updated with