如何在Flask中使用folium和osmnx创建网络地图路线?

0 投票
0 回答
20 浏览
提问于 2025-04-11 22:42

我想用Flask创建一个网站,让用户可以创建路线。我用Folium创建路线没问题,但我需要使用地图上的节点,所以决定用OSMnx。不过,当我使用OSMnx时,网站只显示地图,没有路线。

这是我的代码:

from flask import render_template
import folium
import osmnx as ox
import networkx as nx

@views.route('/', methods=['GET', 'POST'])
def test():
    mapObj = folium.Map(location=[40.748441, -73.985664], zoom_start=15, width=1850, height=900)
    
    ox.config(log_console=True, use_cache=True)

    G_walk = ox.graph_from_place('Manhattan Island, New York City, New York, USA',
                            network_type='walk')
    
    orig_node = ox.nearest_nodes(G_walk, Y=40.748441, X=-73.985664)

    dest_node = ox.nearest_nodes(G_walk, Y=40.748441, X=-73.3)

    route = nx.shortest_path(G_walk,
                            orig_node,
                            dest_node,
                            weight='length')

    route = ox.plot_route_folium(G_walk, route, width=1850, height=910).add_to(mapObj)

    mapObj.get_root().render()

    header = mapObj.get_root().header.render()

    body_html = mapObj.get_root().html.render()

    script = mapObj.get_root().script.render()

    return render_template('home.html', user=current_user, 
                        header=header, body_html=body_html, script=script)

我也试着渲染“路线”,但那样地图也不显示了。

0 个回答

暂无回答

撰写回答