Morteza Hasanabadi
Morteza Hasanabadi

Reputation: 208

How to use custom marker with folium.GeoJson?

I have below simple code with folium.GeoJson, I just want to know how to use custom marker for points or even change the color of them. how to use style_function for this purpose? the docs from Folium don´t have any examples on it

import folium
from folium.plugins import Search

points = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "one"
},
"geometry": {
"type": "Point",
"coordinates": [-71.0636, 42.3581]
}
}
]
}

m = folium.Map(
location=[42.82995815, -74.78991444],
tiles = 'cartodbpositron',
zoom_start=4
)

style_one = lambda x: {'fillColor': '#ffdc30'}
geojson_obj = folium.GeoJson(points, style_function=style_one).add_to(m)

statesearch = Search(layer=geojson_obj,
                     geom_type='Point',
                     placeholder="Search",
                     collapsed=True,
                     search_label='name',
                     search_zoom=14,
                     position='topright'
                    ).add_to(m)

m.save('example.html')

Upvotes: 3

Views: 1588

Answers (1)

jri
jri

Reputation: 475

If you only have markers in your GeoJson object I would suggest to use the built-in Marker class instead of style function. Using these markers you have a variety of option to configure them.

Your code would look like this with the markers:

import folium
from folium.plugins import Search

points = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "one"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [-71.0636, 42.3581]
            }
        }
    ]
}

m = folium.Map(
    location=[42.82995815, -74.78991444],
    tiles = 'cartodbpositron',
    zoom_start=4
)


geojson_obj = folium.GeoJson(points, 
              marker = folium.Marker(icon=folium.Icon(
                                     icon_color='#ff033e',
                                     icon='certificate',
                                     prefix='fa'))      
                        ).add_to(m)

statesearch = Search(layer=geojson_obj,
                     geom_type='Point',
                     placeholder="Search",
                     collapsed=True,
                     search_label='name',
                     search_zoom=14,
                     position='topright'
                    ).add_to(m)

m.save('example.html')

Using the prefix='fa' you can choose an icon from fontawesome (except the ones from v4). Without the prefix you can use all the icons from Bootstrap

Upvotes: 1

Related Questions