5

I want to have a bunch of points in the map, with a red Icon and with some text as a popup when you click on it. I need to use features.GeoJson, because I'll create also a Search on a specific layer, so I can't use features.Marker.

I checked this examples: https://nbviewer.jupyter.org/github/python-visualization/folium/tree/master/examples/ But They don't say what key of the properties dictionary of each point change this color. Regarding the popup, even though I add it as a child it doesn't work.

Here is what I get

Here the code:

import folium
from folium import features
m = folium.Map([0, 0], zoom_start=1)

points_ = {'type': 'FeatureCollection',
 'features': [{'type': 'Feature',
   'properties': {'Codice': 500732, 'Categoria': 'D1', 'Cluster': 3},
   'geometry': {'type': 'Point', 'coordinates': [12.34117475, 45.75345246]},
   'id': '0'},
  {'type': 'Feature',
   'properties': {'Codice': 500732, 'Categoria': 'A2', 'Cluster': 3},
   'geometry': {'type': 'Point', 'coordinates': [12.34117475, 45.75345246]},
   'id': '1'}]}

pp = folium.Popup("hello")
ic = features.Icon(color="red")
gj = folium.GeoJson(points)#, tooltip=tooltip)

gj.add_child(ic)
gj.add_child(pp)
m.add_child(gj)

m

1 Answer 1

1

The standard icon just loads from https://cdn.jsdelivr.net/npm/[email protected]/dist/images/marker-icon.png

So either you can change its color by using something like this

or you can use a different built-in icon instead:

points = {'type': 'FeatureCollection',
 'features': [{'type': 'Feature',
   'properties': {'Codice': 500732, 'Categoria': 'D1', 'Cluster': 3},
   'geometry': {'type': 'Point', 'coordinates': [12.34117475, 45.75345246]},
   'id': '0'},
  {'type': 'Feature',
   'properties': {'Codice': 500732, 'Categoria': 'A2', 'Cluster': 3},
   'geometry': {'type': 'Point', 'coordinates': [12.32117475, 45.72345246]},
   'id': '1'}]}
   
gj = folium.GeoJson(points)

feature_group = folium.FeatureGroup('markers')

for feature in gj.data['features']:
    if feature['geometry']['type'] == 'Point':
        folium.Marker(location=list(reversed(feature['geometry']['coordinates'])),
            icon=folium.Icon(color='red'),
            popup='Hello',
            Categoria=feature['properties']['Categoria']
        ).add_to(feature_group)

feature_group.add_to(m)

Search(
    layer=feature_group,
    search_label="Categoria",
).add_to(m)

m

enter image description here

3
  • 1
    Awesome! You don't know how much Time I spent on this...Although now I cannot add a Search feature. Because the gj is not added to the map, and if I add it I will have to point for each location. How would you add it?
    – M. GENTILI
    Commented Dec 8, 2020 at 19:53
  • I would use FeatureGroup. Let me expand my answer. Commented Dec 9, 2020 at 15:29
  • Awesome!! Thanks :) do you know if there’s any chance to show multiple results, if multiple points on the map have the same property?
    – M. GENTILI
    Commented Dec 10, 2020 at 19:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.