ipyleaflet
ipyleaflet copied to clipboard
Handling location change on draggable Markers
Hi, I created a script for an interactive map to find shortest path between 2 locations based on https://ipyleaflet.readthedocs.io/en/latest/usage.html?highlight=observe and https://git.io/J6y6O This is how I handle change in marker position:
from_marker = Marker(location=current_pos, draggable=True)
to_marker = Marker(location=current_pos, icon=to_marker_style, draggable=True)
path_layer_list = []
def handle_change_location(event, marker):
event_owner = event['owner']
event_owner.nearest_node = ox.get_nearest_node(graph, event_owner.location)
marker.neares_node = ox.get_nearest_node(graph, marker.location)
shortest_path = nx.dijkstra_path(graph, event_owner.nearest_node, marker.neares_node,
weight='length')
if len(path_layer_list) == 1:
m.remove_layer(path_layer_list[0])
path_layer_list.pop()
shortest_path_points = nodes.loc[shortest_path]
print(shortest_path_points) # PRINT SHORTEST PATH
path = gpd.GeoDataFrame([LineString(shortest_path_points.geometry.values)], columns=['geometry'])
path.to_file('dataframe.shp')
path_layer = GeoData(geo_dataframe=path, style={'color':'blue', 'weight':2})
m.add_layer(path_layer)
path_layer_list.append(path_layer)
from_marker.observe(lambda event: handle_change_location(event, to_marker), 'location')
to_marker.observe(lambda event: handle_change_location(event, from_marker), 'location')
Everything looks good but I need to access the shortest_path_points for my navigation application. I can't access or print any value in the handle_change_location function. Any idea on how it should be done? Thanks!