Python|使用 folium 包绘制谷歌地图
Folium建立在Python生态系统的数据整理优势和 Leaflet.js (JavaScript) 库的映射优势之上。简单地说,在Python中操作您的数据,然后通过 Folium 在传单地图上将其可视化。 Folium 可以轻松地在交互式 Leaflet 地图上可视化在Python中操作的数据。这个库有许多来自 OpenStreetMap、Mapbox 等的内置图块集。
安装folium模块的命令:
pip install folium
代码 #1:创建基本地图。
Python3
# import folium package
import folium
# Map method of folium return Map object
# Here we pass coordinates of Gfg
# and starting Zoom level = 12
my_map1 = folium.Map(location = [28.5011226, 77.4099794],
zoom_start = 12 )
# save method of Map object will create a map
my_map1.save(" my_map1.html " )
Python3
# import folium package
import folium
my_map2 = folium.Map(location = [28.5011226, 77.4099794],
zoom_start = 12)
# CircleMarker with radius
folium.CircleMarker(location = [28.5011226, 77.4099794],
radius = 50, popup = ' FRI ').add_to(my_map2)
# save as html
my_map2.save(" my_map2.html ")
Python3
# import folium package
import folium
my_map3 = folium.Map(location = [28.5011226, 77.4099794],
zoom_start = 15)
# Pass a string in popup parameter
folium.Marker([28.5011226, 77.4099794],
popup = ' Geeksforgeeks.org ').add_to(my_map3)
my_map3.save(" my_map3.html ")
Python3
# import folium package
import folium
my_map4 = folium.Map(location = [28.5011226, 77.4099794],
zoom_start = 12)
folium.Marker([28.704059, 77.102490],
popup = 'Delhi').add_to(my_map4)
folium.Marker([28.5011226, 77.4099794],
popup = 'GeeksforGeeks').add_to(my_map4)
# Add a line to the map by using line method .
# it connect both coordinates by the line
# line_opacity implies intensity of the line
folium.PolyLine(locations = [(28.704059, 77.102490), (28.5011226, 77.4099794)],
line_opacity = 0.5).add_to(my_map4)
my_map4.save("my_map4.html")
输出 :
代码 #2:添加带有弹出文本的圆形标记。
Python3
# import folium package
import folium
my_map2 = folium.Map(location = [28.5011226, 77.4099794],
zoom_start = 12)
# CircleMarker with radius
folium.CircleMarker(location = [28.5011226, 77.4099794],
radius = 50, popup = ' FRI ').add_to(my_map2)
# save as html
my_map2.save(" my_map2.html ")
输出 :
代码 #3:为降落伞样式标记添加一个 simple_marker 并带有弹出文本。
Python3
# import folium package
import folium
my_map3 = folium.Map(location = [28.5011226, 77.4099794],
zoom_start = 15)
# Pass a string in popup parameter
folium.Marker([28.5011226, 77.4099794],
popup = ' Geeksforgeeks.org ').add_to(my_map3)
my_map3.save(" my_map3.html ")
输出 :
代码 #4:在地图上添加一条线
Python3
# import folium package
import folium
my_map4 = folium.Map(location = [28.5011226, 77.4099794],
zoom_start = 12)
folium.Marker([28.704059, 77.102490],
popup = 'Delhi').add_to(my_map4)
folium.Marker([28.5011226, 77.4099794],
popup = 'GeeksforGeeks').add_to(my_map4)
# Add a line to the map by using line method .
# it connect both coordinates by the line
# line_opacity implies intensity of the line
folium.PolyLine(locations = [(28.704059, 77.102490), (28.5011226, 77.4099794)],
line_opacity = 0.5).add_to(my_map4)
my_map4.save("my_map4.html")
输出 :