我正在尝试使用folium在我的django应用中获取地图。
代码如下:
def map(request):
data = pd.DataFrame({
'lat': [77, 75, 72, 77, 78],
'lon': [28, 26, 19, 29, 30],
'name': ['Buenos Aires', 'jaipur', 'mumbai', 'gurgaon', 'dehradun']
})
m = folium.Map(location=[21, 78], tiles="Mapbox Bright", zoom_start=4.75)
for i in range(0, len(data)):
folium.Marker([data.iloc[i]['lon'], data.iloc[i]['lat']], popup=data.iloc[i]['name']).add_to(m)
m.save('graph.html')
context = {'map': m}
print("context is", context)
return render(request, 'classroom/teachers/map.html', context)
Urls.py
path('map', teachers.map, name='map'),
HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ map.render }}
</body>
我希望它会在给定的纬度和经度上显示带有一些标记的地图,但是出了点问题。我需要更改以获得预期的结果吗?
答案 0 :(得分:0)
先导入大熊猫和大叶草,然后尝试遵循这种模式
import folium
import pandas
data = pandas.read_csv("Volcanoes_USA.txt")
lat = list(data["LAT"])
lon = list(data["LON"])
elev = list(data["ELEV"])
def color_producer(elevation):
if elevation < 1000:
return 'green'
elif 1000 <= elevation < 3000:
return 'orange'
else:
return 'red'
map=folium.Map(location=[38.58,-99.09],zoom_start=6,tiles="Mapbox Bright")
fg = folium.FeatureGroup(name="My Map")
for lt, ln, el in zip(lat, lon, elev):
fg.add_child(folium.CircleMarker(location=[lt, ln], radius=6, popup=str(el) + "m",
fill_color = color_producer(el), color='grey', fill_opacity=0.7))
fg.add_child(folium.GeoJson(data=open('world.json','r',encoding='utf-8-sig').read()),
style_function = lambda x: {'fillColor': 'yellow'})
map.add_child(fg)
map.save("Map1.html")