I have a basic folium heatmap that shows locations as CircleMarker and a HeatMap layer on top as displayed below.

enter image description here

I wanted to add search functionality in my map so I converted my pandas dataframe into a GeoJson format so that I can pass that.

class folium.plugins.Search(layer, search_label=None, search_zoom=None, geom_type='Point', position='topleft', placeholder='Search', collapsed=False, **kwargs) Bases: branca.element.MacroElement

Adds a search tool to your map.

Parameters: layer (GeoJson, TopoJson, FeatureGroup, MarkerCluster class object.) – The map layer to index in the

I was able to convert my Pandas DataFrame into GeoJson using below code.

df_json = pd.read_csv("C:\\py\\folium\\NE Task 1\\json.csv").dropna(how="any")

# convert lat-long to floats and change address from ALL CAPS to Regular Capitalization
df_json['latitude'] = df_json['latitude'].astype(float)
df_json['longitude'] = df_json['longitude'].astype(float)
df_json['Site Name'] = df_json['Site Name'].str.title()


# we don't need all those columns - only keep useful ones
useful_cols = ['Site ID', 'Site Name', 'latitude', 'longitude']
df_subset = df_json[useful_cols]

# drop any rows that lack lat/long data
df_geo = df_subset.dropna(subset=['latitude', 'longitude'], axis=0, inplace=False)


def df_to_geojson(df_json, properties, lat='latitude', lon='longitude'):

    geojson = {'type': 'FeatureCollection', 'features': []}

    # loop through each row in the dataframe and convert each row to geojson format
    for _, row in df_json.iterrows():
        # create a feature template to fill in
        feature = {'type': 'Feature',
                   'properties': {},
                   'geometry': {'type': 'Point', 'coordinates': []}}


        # fill in the coordinates
        feature['geometry']['coordinates'] = [row[lon], row[lat]]

        # for each column, get the value and add it as a new feature property
        for prop in properties:
            feature['properties'][prop] = row[prop]

        # add this feature (aka, converted dataframe row) to the list of features inside our dict
        geojson['features'].append(feature)

    return geojson


geojson_dict = df_to_geojson(df_geo, properties=useful_cols)
geojson_str = json.dumps(geojson_dict, indent=2)


folium.plugins.Search(data=geojson_dict, geom_type='Point',
                      search_zoom=14, search_label='Site ID').add_to(map)

After doing that search function is working properly as i wanted but a Marker is being displayed on TOP which i cannot hide like below.

enter image description here

Please help me guide how can I hide this marker and keep GeoJson intact so that I can use it for search function. I tried to make it transparent, change the opacity of GeoJson through the solutions I found over stackOverflow but nothing works.

Thanks in advance for your time and sorry for the long post.

Best Regards

1 Answers

0
Vineet Sansi On

In general,

if you add folium.LayerControl().add_to(map) to the map then it provides functionality to display or hide your Geojson markers. Then you can hide or display markers using show=False or from 'Layers' icon on the top right of the map (see the image below)

For example:

# creating folium GeoJson objects from out GeoDataFrames
pointgeo = folium.GeoJson(gdf,name='group on map', show=False,
                      tooltip=folium.GeoJsonTooltip(fields=['Name', 'Relation', 'City'], aliases=['Name','Relation', 'City'],
                        localize=True)).add_to(map)

# To Add a LayerControl add below line
folium.LayerControl().add_to(map)

map

Like in these images Marker are shown or hidden by controlling layer from top righ 'Layers' - 1) with markers 2) markers hidden

with markers

hidden markers

you can refer to this link for more example: Folium_search

Hope this helps!!