gpt4 book ai didi

python - 将数据库中的所有纬度和经度标记放置到来自 sql 结果的 Openstreet/Leaflet map 上

转载 作者:行者123 更新时间:2023-12-01 07:27:40 25 4
gpt4 key购买 nike

我能够从我的数据库表中根据查询结果将一个标记放置到 map 上。问题是当我的结果超过一个时,我无法在 map 上放置多个标记。所以基本上,我想将 db 中 sql 查询的所有纬度和经度标记放置到 OSM/Leaflet MAP 上。

我见过类似的例子,但是编码不太一样。

def search_all(self):

print ('Searching All...')

looking = str(self.lookall.text())

conn = Connection to DB

cursor = conn.cursor()
query = "SELECT*FROM Relevant Tables"
cursor.execute(query)
results = cursor.fetchall()
self.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(results):
self.tableWidget.insertRow(row_number)
for column_number, data in enumerate (row_data):
self.tableWidget.setItem(row_number,column_number,QtWidgets.QTableWidgetItem(str(data)))

pass
pass

for i in range(1,len(results)):


lat = results[i][5]
long = results[i][6]]

print (lat)
print (long)

结果打印如下:

-33.863611
18.908333
-33.046667
18.677222
-33.046667
18.677222
-33.046667
18.677222
-33.046667
18.677222
-33.863611
18.913889
-33.863611
18.913889
-33.883611
18.927778
-33.883611
18.927778
-33.883611
18.927778
-33.883611
18.927778
-33.866667
18.916667
-33.866667
18.916667
-33.901389
18.775
-33.901389
18.775
-33.866667
18.916667
-33.866667
18.916667
-33.866667
18.916667
-33.866667
18.916667
        html = """<!DOCTYPE html>
<html>
<head>
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css"
integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ=="
crossorigin=""
/>
<script
src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"
integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw=="
crossorigin=""
></script>
</head>
<body>
<div id="mapDiv" style="width: 1300px; height: 500px"></div>

<script>
var lat = { lat };
var long = { long };
var len = { lent };
var i = 0;

for (var i = 0; i < { lent }; i++) {{
marker = new L.marker({ lat }, { lon }).addTo(map);
}}

map = L.map("mapDiv").setView([lat, lon], 13);

L.tileLayer("https://tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png", {{
attribution:
'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors',

maxZoom: 20
}}).addTo(map);

// add marker to the map

marker = L.marker([lat, lon]).addTo(map);
</script>
</body>
</html>"""

os2 = html.format(lat = lat,lon = lon , len = lent)

self.view.setHtml(os2)```

我可以从 DB 的 SQL 查询中获取经纬度。我不太确定如何将其实现到 OSM 的 HTML 代码中。我所做的字符串格式化似乎没有达到目的。由于没有更新 map

最佳答案

本例的想法是连接坐标:

# ...
coordinates = []
for result in results:
coordinates.append((result[5], result[6]))

lat_center = sum([coordinate[0] for coordinate in coordinates]) / len(
coordinates
)
lng_center = sum([coordinate[1] for coordinate in coordinates]) / len(
coordinates
)

html = r"""
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html { height: 100%; }
body { height: 100%; margin: 0; padding: 0 }
#mapid { height: 100% }
</style>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js" integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw==" crossorigin=""></script>
</head>
<body>
<div id="mapDiv" style="width:100%; height:100%"></div>
<script>
"""
html += "var map = L.map('mapDiv').setView([{lat}, {lng}], 10);".format(
lat=lat_center, lng=lng_center
)
html += """
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
"""

for latitude, longitude in coordinates:
html += "L.marker([{lat}, {lng}]).addTo(map);\n".format(
lat=latitude, lng=longitude
)

html += "</script> </body> </html>"
self.view.setHtml(html)

输出:

enter image description here

如果你想更优雅地做到这一点,你可以使用 Jinja2或更好folium

关于python - 将数据库中的所有纬度和经度标记放置到来自 sql 结果的 Openstreet/Leaflet map 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57376696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com