-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Multiple markers with one icon #2068
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 2 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 |
|---|---|---|
|
|
@@ -275,7 +275,6 @@ class Icon(MacroElement): | |
| var {{ this.get_name() }} = L.AwesomeMarkers.icon( | ||
| {{ this.options|tojavascript }} | ||
| ); | ||
| {{ this._parent.get_name() }}.setIcon({{ this.get_name() }}); | ||
| {% endmacro %} | ||
| """ | ||
| ) | ||
|
|
@@ -372,6 +371,20 @@ class Marker(MacroElement): | |
| """ | ||
| ) | ||
|
|
||
| class SetIcon(MacroElement): | ||
| """Set the icon of a marker after both are created.""" | ||
| _template = Template(""" | ||
| {% macro script(this, kwargs) %} | ||
| {{ this.marker.get_name() }}.setIcon({{ this.icon.get_name() }}); | ||
| {% endmacro %} | ||
| """) | ||
|
|
||
| def __init__(self, marker: 'Marker', icon: 'Icon'): | ||
| super().__init__() | ||
| self._name = "SetIcon" | ||
| self.marker = marker | ||
| self.icon = icon | ||
|
|
||
| def __init__( | ||
| self, | ||
| location: Optional[Sequence[float]] = None, | ||
|
|
@@ -389,7 +402,6 @@ def __init__( | |
| ) | ||
| if icon is not None: | ||
| self.add_child(icon) | ||
| self.icon = icon | ||
| if popup is not None: | ||
| self.add_child(popup if isinstance(popup, Popup) else Popup(str(popup))) | ||
| if tooltip is not None: | ||
|
|
@@ -410,6 +422,9 @@ def render(self): | |
| raise ValueError( | ||
| f"{self._name} location must be assigned when added directly to map." | ||
| ) | ||
| for child in list(self._children.values()): | ||
|
Collaborator
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 looks strange to me. A marker can have only one
Member
Author
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.
That would not work for the case where the icon is added using By looking through the children, we catch the icon whichever way it was added. |
||
| if isinstance(child, Icon): | ||
| self.add_child(self.SetIcon(marker=self, icon=child)) | ||
| super().render() | ||
|
|
||
|
|
||
|
|
||
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.
Why did the above code not work?
Uh oh!
There was an error while loading. Please reload this page.
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.
It worked I think, but it's not necessary.
Figure.scriptis an OrderedDict, so we are already guaranteed that multiple renders of the same Icon object will result in only one entry inFigure.script.MacroElement.render, that one Icon object is rendered multiple timesFigure.scriptwith the Icon object name as key. So each render overwrites the previous one, and the result is the Icon object appears in the final output only once.