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

DivIcon transparent background #798

Closed
giswqs opened this issue Mar 3, 2021 · 11 comments
Closed

DivIcon transparent background #798

giswqs opened this issue Mar 3, 2021 · 11 comments

Comments

@giswqs
Copy link
Contributor

giswqs commented Mar 3, 2021

The DivIcon has a white background. Is it possible to make it transparent? Thanks.

Relevant issue: #792 @deeplook

from ipyleaflet import Marker, DivIcon, Map

center = (52.204793, 360.121558)

m = Map(center=center, zoom=10)
icon = DivIcon(html='foo bar', bg_pos=[0, 0], icon_size=[150, 150])
mark = Marker(location=center, icon=icon)
m.add_layer(mark);

m

@deeplook
Copy link
Contributor

deeplook commented Jul 1, 2021

I'm using these two cells for now as a workaround:

%%html
<style>
.leaflet-div-icon { background-color: transparent; border-color: transparent }
</style>
from ipyleaflet import DivIcon, Map, Marker

m = Map(center=(0, 0), zoom=1)
w, h = 32, 32
html = f'<img src="https://simpleicons.org/icons/apple.svg" width="{w}" height="{h}"/>'
icon = DivIcon(html=html, bg_pos=(0, 0), icon_size=[w, h])
apple_loc = (37.3351901, -122.0114123)
mark = Marker(location=apple_loc, icon=icon, draggable=True)
m.add_layer(mark)
m

Screenshot 2021-07-01 at 10 55 41

@giswqs
Copy link
Contributor Author

giswqs commented Jul 1, 2021

@deeplook Thank you for sharing the workaround. The html magic command will only work in Jupyter notebook. To integrate this into leafmap, we need a workaround that works in a Python script.

@martinRenou
Copy link
Member

You could maybe import from IPython.display import HTML and display it using from IPython.display import display. But that's a hacky solution. It would be nicer using a real ipyleaflet solution.

@deeplook
Copy link
Contributor

Works too! Here's a EM Soccer Finals 2021 Supporter Map (after some Twitter postings by others), ca. 25 LOC.

Screenshot 2021-07-11 at 17 49 29

@giswqs
Copy link
Contributor Author

giswqs commented Dec 13, 2021

@deeplook Do you have the sample code for this? I am trying to integrate this into geemap for labeling features. gee-community/geemap#815

@deeplook
Copy link
Contributor

@giswqs It took some time to undust it, but yes! Here it is cell by cell. I hope it's useful:

import requests
from ipyleaflet import basemaps, DivIcon, GeoJSON, Map, Marker
from IPython.display import HTML
from shapely.geometry import shape
# Workaround for cleaning the icon background (must be on a separate cell):
HTML("""<style>
.leaflet-div-icon { background-color: transparent; border-color: transparent }
</style>""")
m = Map(center=([53.33, 7.25]), zoom=3.5, basemap=basemaps.CartoDB.Positron)

url = "https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json"
data = requests.get(url).json()
data2 = {feat["id"]: feat["geometry"] for feat in data["features"]}

w, h = 32, 32
url_base = "https://upload.wikimedia.org/wikipedia/en"
for cc3 in 'GBR ALB EST LTU LVA UKR BLR CHE AUT SVN SVK SRB BGR CZE BIH NOR ISL GRC HRV MKD MDA SWE ESP DEU DNK BEL LUX NLD FRA ITA ROU POL HUN PRT FIN IRL'.split():
    geometry = data2[cc3]
    sh = shape(geometry)
    lat, lon = (61.81, 9.05) if cc3 == "NOR" else (sh.centroid.y, sh.centroid.x)
    url = (url_base + "/b/be/Flag_of_England.svg") if cc3 == "GBR" else (url_base + "/0/03/Flag_of_Italy.svg")
    html = f'<img src="{url}" width="{w}" height="{h}"/>'
    icon = DivIcon(html=html, bg_pos=(0, 0), icon_size=[w, h])
    m.add_layer(Marker(location=(lat, lon), icon=icon, draggable=False))
    m += GeoJSON(data=geometry, style={"fill": None})

m.layout.height = "550px"
m

@giswqs
Copy link
Contributor Author

giswqs commented Dec 13, 2021

@deeplook Great! Thanks for sharing. I will try it out.

@deeplook
Copy link
Contributor

My pleasure!

@giswqs
Copy link
Contributor Author

giswqs commented Jan 22, 2022

This has been implemented in leafmap. See notebook example https://leafmap.org/notebooks/36_add_labels

add_labels

@giswqs giswqs closed this as completed Jan 22, 2022
@martinRenou
Copy link
Member

martinRenou commented Jan 22, 2022

If you don't mind, we could keep this issue open until this is properly implemented in ipyleaflet 😊

@martinRenou martinRenou reopened this Jan 22, 2022
@giswqs giswqs closed this as completed Jun 5, 2023
@fgashakamba
Copy link

@martinRenou Do you know if any solution has been worked on? I am having same issue until now.

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

No branches or pull requests

4 participants