Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbers in map marker in Folium

Tags:

folium

i want to display some geo locations on map, but i want the map-pin icon to display numbers instead of the default map pin. Is there any way to do that?

I checked in font awesome icons but it didn't work.

Below is my code:

import folium
m = folium.Map(
    location=[45.3288, -121.6625],
    zoom_start=12,
    #tiles='Mapbox Bright'
)

folium.Marker([45.3288, -121.6625], popup='<i>Mt. Hood Meadows</i>').add_to(m)
folium.Marker([45.3311, -121.7113], popup='<b>Timberline Lodge</b>',icon=folium.Icon(color='red')).add_to(m)
m

enter image description here

What i want is instead of this default map marker i want to include numbers in my marker instead of info-sign

Something like this:

enter image description here

i couldn't find the answer anywhere. Any leads on this?

display number 1 to 9 inside map marker pin

like image 416
Shubham R Avatar asked Sep 25 '17 08:09

Shubham R


2 Answers

I was attempting something similar on a recent project and this is what I came up with. Might work for you.

It plots a DivCon marker with html and then a transparent circle marker in the same location.

import folium
from folium.features import DivIcon

m = folium.Map(
    location=[45.3288, -121.6625],
    zoom_start=12,
    #tiles='Mapbox Bright'
)

p1 = [45.3288, -121.6625]
folium.Marker(p1, icon=DivIcon(
        icon_size=(150,36),
        icon_anchor=(7,20),
        html='<div style="font-size: 18pt; color : black">1</div>',
        )).add_to(m)
m.add_child(folium.CircleMarker(p1, radius=15))

p2 = [45.3311, -121.7113]
folium.Marker(p2, icon=DivIcon(
        icon_size=(150,36),
        icon_anchor=(7,20),
        html='<div style="font-size: 18pt; color : black">2</div>',
        )).add_to(m)
m.add_child(folium.CircleMarker(p2, radius=15))

enter image description here

like image 186
Bob Haffner Avatar answered Sep 19 '22 13:09

Bob Haffner


Adapting the previous answer by @bob I found a solution that fit my needs. I put it below in case it is useful for anyone:

import folium
from folium.features import DivIcon

def number_DivIcon(color,number):
    """ Create a 'numbered' icon
    
    """
    icon = DivIcon(
            icon_size=(150,36),
            icon_anchor=(14,40),
#             html='<div style="font-size: 18pt; align:center, color : black">' + '{:02d}'.format(num+1) + '</div>',
            html="""<span class="fa-stack " style="font-size: 12pt" >>
                    <!-- The icon that will wrap the number -->
                    <span class="fa fa-circle-o fa-stack-2x" style="color : {:s}"></span>
                    <!-- a strong element with the custom content, in this case a number -->
                    <strong class="fa-stack-1x">
                         {:02d}  
                    </strong>
                </span>""".format(color,number)
        )
    return icon

col_hex = ['#440154',
 '#481a6c',
 '#472f7d',
 '#414487',
 '#39568c',
 '#31688e',
 '#2a788e',
 '#23888e',
 '#1f988b',
 '#22a884',
 '#35b779',
 '#54c568',
 '#7ad151',
 '#a5db36',
 '#d2e21b']

num = 0
loc = (43.613, 3.888)

fm = folium.Map(location=loc, tiles="Stamen Terrain")
folium.Marker(
        location=loc,
        popup="Delivery " + '{:02d}'.format(num+1),
        icon=folium.Icon(color='white',icon_color='white'),
        markerColor=col_hex[num],
    ).add_to(fm)
folium.Marker(
        location=loc,
        popup="Delivery " + '{:02d}'.format(num+1),
        icon= number_DivIcon(col_hex[num],num+1)
    ).add_to(fm)

fm

map with colored numbered marker

like image 33
lhoupert Avatar answered Sep 22 '22 13:09

lhoupert