Skip to content
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

Multiple markers with one icon #2068

Merged

Conversation

Conengmo
Copy link
Member

@Conengmo Conengmo commented Jan 1, 2025

Based on the comments on #2053 I made a new version that sets the icon from the markers perspective. This is also similar to how Layer works.

  • Check if a marker has an icon added as a child, if so, add another element that sets the icon on the marker.

Tested it with this case:

    m = Map((44, -93), tiles="cartodb dark matter", zoom_start=8)

    icon = Icon(icon='music')
    markers = [
        Marker((52, 5), icon=icon, popup="0"),
        Marker((52, 5.1), icon=icon, popup="1"),
        Marker((52.1, 5)),
        Marker((52.1, 5.1)),
    ]
    Icon(icon="cloud").add_to(markers[-1])
    fg = FeatureGroup(name='group').add_to(m)
    for marker in markers:
        marker.add_to(fg)

    FitOverlays(padding=10).add_to(m)

If this approach looks good, I'll generate some test cases.

Not sure how much of a breaking change this is. From a direct user perspective, I don't think there's an issue. Not sure about packages that use Folium internals though.

@hansthen
Copy link
Collaborator

hansthen commented Jan 1, 2025

Looks good to me.

# this makes sure it is added only once
self._parent.add_child(icon, name=icon.get_name(), index=0)
self.icon = icon
self.add_child(icon)
Copy link
Collaborator

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?

Copy link
Member Author

@Conengmo Conengmo Jan 1, 2025

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.script is an OrderedDict, so we are already guaranteed that multiple renders of the same Icon object will result in only one entry in Figure.script.

  • Multiple markers may now have the same Icon object as child.
  • In the first rendering pass, of MacroElement.render, that one Icon object is rendered multiple times
  • Those multiple renders are added to Figure.script with 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.

if self.location is None:
raise ValueError(
f"{self._name} location must be assigned when added directly to map."
)
for child in list(self._children.values()):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks strange to me. A marker can have only one Icon, correct? Why do we need to look through all the children? Would it not be easier to use self.icon?

Copy link
Member Author

@Conengmo Conengmo Jan 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be easier to use self.icon?

That would not work for the case where the icon is added using marker.add_child(icon) or icon.add_to(marker).

By looking through the children, we catch the icon whichever way it was added.

@hansthen
Copy link
Collaborator

hansthen commented Jan 1, 2025

In general I have no objections, but I do have two comments that you may want to look at.

@Conengmo
Copy link
Member Author

Conengmo commented Jan 1, 2025

Thanks for the review! I replied to your comments, let me know if you have further questions or suggestions!

@Conengmo Conengmo changed the title Multiple markers with one icon (take 2) Multiple markers with one icon Jan 1, 2025
@hansthen hansthen merged commit bdbf7ed into python-visualization:main Jan 2, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants