0

I want to create an onclick event listener to my folium map which is currently marked with locations and popup being the location name and whenever the marker is clicked i want to retrieve the popup and get the popup flows from another data frame and draw the ant path for those flows. Example: if the marker is clicked on location x it needs to fetch all the flows from the other data frame say df2 like x to y, x to z and after fetching all those flows it needs to create an ant path for those flows fetched. can anyone create a sample code for the same thanks :) It needs to create an antiflow for those locations fetched when a marker is clicked

import pandas as pd
import folium
from folium.plugins import HeatMap
from folium.map import Marker, Template
def on_marker_click(e):
    popup = e.target.get_popup()
    location_name = popup.get_children()[0].get_content()
    flows = df2[df2['stock_loc_id_x'] == location_name]
    if len(flows) > 0:
        points = []
        for index, row in flows.iterrows():
            points.append([row['LATITUDE1'], row['LONGITUDE1']])
            points.append([row['LATITUDE2'], row['LONGITUDE2']])
        folium.PolyLine(points, color='red', tooltip='<br>'.join([f'{col}: {{flows["{col}"]}}' for col in ['ND_Score', 'SD_Score', 'ND_Volume', 'SD_Volume']]),
                        smooth_factor=1.0).add_to(map)

map = folium.Map(prefer_canvas=True)

def plotDot(point):
    folium.Marker(location=[point.LATITUDE1, point.LONGITUDE1],
                        radius=2,
                        color='blue',
                        popup=folium.Popup(point.stock_loc_id),
                        weight=5,
                        ).add_to(map)


gps.apply(plotDot, axis=1)
map
folium.TileLayer('CartoDB dark_matter').add_to(map)
folium.LayerControl().add_to(map)
map
Sri
  • 1
  • 1
  • I did not understand every part of your question and I think you better start to code and get back here with a concrete problem. Like described here https://meta.stackoverflow.com/a/334823/13843906 try to "Make a good faith attempt to solve the problem yourself first" and "Ask about specific problems with your existing implementation". Nevertheless, you will probably need something like this: https://stackoverflow.com/questions/74436743/show-path-in-folium-map-by-clicking-or-hovering-marker – flipSTAR May 16 '23 at 13:16
  • thanks for the response i tried with the above code and tried editing the marker template as u r using in your previous post but didnt get the solution for those can u pls help here? – Sri May 18 '23 at 13:45
  • i have added the sample code above can u pls help here @flipSTAR – Sri May 18 '23 at 13:49
  • @PeaceLeka can you help here please? – Sri May 26 '23 at 17:15
  • hey, is it right, that you want to show a specific antpath when a marker is clicked? the linked question already contains that code. please adapt it for you. If you cant use it or have a specific problem please describe why/what is the problem - any error messages ? or did I not get your question? what is "antiflow" ? Is the path linked to a marker changing? your code snippet just creates a map with markers (if you add gps data ...) and did not help me to identify your problem, sorry – flipSTAR May 28 '23 at 23:09
  • Hi sorry for your confusion. let me explain in detail i have two dataframes df1 with three columns lat and long and name of those lat and long and df2 with all the names flow example if df1 has name chennai df2 will have the flow chennai -> madurai chennai->tirunelveli chennai -> bangalore and some more information in df2. – Sri May 29 '23 at 05:47
  • that is all about dataframes. what i want to achieve is i want to create a map with df1 lat and long and assign the popup as names of df1 and whenever the marker is clicked it needs to fetch all the corresponding flows from df1 and create a path flow for that with popup as the information from df2 @flipSTAR – Sri May 29 '23 at 05:52
  • in your linked answer whenever the marker is clicked it is fetching the lat and long of marker and fetching the corresponding flow from other df but for me there is no lat and long in other df only the matching column is name with both df so i want to make use of this with popup. or suggest me some other way thanks in advance :) – Sri May 29 '23 at 05:54
  • if you do not have lat/long data in your df2 you cant create a path. You would have to get the gps data of the flow before creating the map and link it to the popup. I do not know a way to dynamically adding geo data. If you just want to add textual informations in your popup you have to attach them to the marker while creating the map as a html string. – flipSTAR May 31 '23 at 14:42
  • thanks for the input but my df1 is already having lat and long for the location in df2 i thought will mark the marker with location in popup and retrieve those popup data and check with df2 to get the flow – Sri May 31 '23 at 15:36
  • any lead or sample code on how to get the popup data on onclick ? – Sri May 31 '23 at 15:37

0 Answers0