从 SQLite3 向 folium 地图添加标记 table
Add Markers to folium map from SQLite3 table
我正在尝试在 folium 地图上放置许多标记。坐标是从 SQLite3 Table 绘制的,但现在没有显示地图,也没有抛出任何错误。
def maps():
melbourne = (-37.840935, 144.946457)
map = folium.Map(location = melbourne)
try:
sqliteConnection = sqlite3.connect('25july_database.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
sqlite_select_query = """SELECT latitude, longitude FROM test555;"""
cursor.execute(sqlite_select_query)
items = cursor.fetchall()
for item in items:
folium.Marker(location = item)
cursor.close()
except sqlite3.Error as error:
print("Failed to read data from sqlite table", error)
finally:
if (sqliteConnection):
sqliteConnection.close()
print("The SQLite connection is closed")
我试图将“项目”设为一个列表 folium.Marker(location = [item])
但是抛出了以下错误 ValueError: Expected two (lat, lon) values for location, instead got: [(-37.7650309, 144.9613659)].
这向我表明变量没有错,但其他地方有问题。
提前致谢!
为了从列表中提取元组 (-37.7650309, 144.9613659)
,您只需要获取第一个元素:folium.Marker(location = item[0])
您还需要将标记添加到地图中:folium.Marker(location = item[0]).add_to(map)
为了绘制地图,您需要 return 在函数的末尾。
你会有这样的东西(它在我的 Jupyter Notebook 中有效):
def maps():
melbourne = (-37.840935, 144.946457)
map = folium.Map(location = melbourne)
try:
sqliteConnection = sqlite3.connect('25july_database.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
sqlite_select_query = """SELECT latitude, longitude FROM test555;"""
cursor.execute(sqlite_select_query)
items = cursor.fetchall()
for item in items:
folium.Marker(location = item[0]).add_to(map)
cursor.close()
except sqlite3.Error as error:
print("Failed to read data from sqlite table", error)
finally:
if (sqliteConnection):
sqliteConnection.close()
print("The SQLite connection is closed")
return map
N.B:
您不应该使用 map
作为变量的名称,因为您隐藏了 Python 标准库的 map()
函数。
我正在尝试在 folium 地图上放置许多标记。坐标是从 SQLite3 Table 绘制的,但现在没有显示地图,也没有抛出任何错误。
def maps():
melbourne = (-37.840935, 144.946457)
map = folium.Map(location = melbourne)
try:
sqliteConnection = sqlite3.connect('25july_database.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
sqlite_select_query = """SELECT latitude, longitude FROM test555;"""
cursor.execute(sqlite_select_query)
items = cursor.fetchall()
for item in items:
folium.Marker(location = item)
cursor.close()
except sqlite3.Error as error:
print("Failed to read data from sqlite table", error)
finally:
if (sqliteConnection):
sqliteConnection.close()
print("The SQLite connection is closed")
我试图将“项目”设为一个列表 folium.Marker(location = [item])
但是抛出了以下错误 ValueError: Expected two (lat, lon) values for location, instead got: [(-37.7650309, 144.9613659)].
这向我表明变量没有错,但其他地方有问题。
提前致谢!
为了从列表中提取元组 (-37.7650309, 144.9613659)
,您只需要获取第一个元素:folium.Marker(location = item[0])
您还需要将标记添加到地图中:folium.Marker(location = item[0]).add_to(map)
为了绘制地图,您需要 return 在函数的末尾。
你会有这样的东西(它在我的 Jupyter Notebook 中有效):
def maps():
melbourne = (-37.840935, 144.946457)
map = folium.Map(location = melbourne)
try:
sqliteConnection = sqlite3.connect('25july_database.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")
sqlite_select_query = """SELECT latitude, longitude FROM test555;"""
cursor.execute(sqlite_select_query)
items = cursor.fetchall()
for item in items:
folium.Marker(location = item[0]).add_to(map)
cursor.close()
except sqlite3.Error as error:
print("Failed to read data from sqlite table", error)
finally:
if (sqliteConnection):
sqliteConnection.close()
print("The SQLite connection is closed")
return map
N.B:
您不应该使用 map
作为变量的名称,因为您隐藏了 Python 标准库的 map()
函数。