我需要你用folium和pyqt5开发一个能够显示多个航迹的软件,并且只显示最新的航迹参数,去除旧的参数
时间: 2024-03-18 07:43:50 浏览: 30
好的,我可以使用folium和PyQt5开发一个能够显示多个航迹的软件,并且只显示最新的航迹参数,去除旧的参数。以下是代码示例:
```python
import sys
import folium
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
class MapWindow(QMainWindow):
def __init__(self):
super().__init__()
# 设置窗口标题和大小
self.setWindowTitle('多个航迹地图')
self.setGeometry(100, 100, 800, 600)
# 创建WebEngineView对象
self.web = QWebEngineView(self)
self.web.load(QUrl('about:blank')) # 加载空白页面
# 添加WebEngineView到主窗口
self.setCentralWidget(self.web)
# 创建地图对象
self.map_obj = folium.Map(location=[0, 0], zoom_start=2)
# 创建航迹数据
self.flight_paths = [
{'start': [37.615223, 55.752820], 'end': [40.712776, -74.005974], 'color': 'blue', 'weight': 2},
{'start': [31.230416, 121.473701], 'end': [35.689487, 139.691711], 'color': 'red', 'weight': 3},
{'start': [-33.865143, 151.209900], 'end': [-34.603722, -58.381592], 'color': 'green', 'weight': 4},
]
# 添加航迹到地图上
for path in self.flight_paths:
start = path['start']
end = path['end']
color = path['color']
weight = path['weight']
folium.PolyLine(locations=[start, end], color=color, weight=weight).add_to(self.map_obj)
# 调整地图中心和缩放级别
self.map_obj.fit_bounds([[18.0, 110.0], [35.0, 125.0]])
self.map_obj.zoom_start = 6
# 将地图渲染为HTML字符串
self.html = self.map_obj._repr_html_()
# 加载地图到WebEngineView
self.web.setHtml(self.html)
def add_latest_path(self, start, end, color, weight):
# 添加最新航迹参数
self.flight_paths.clear()
self.flight_paths.append({'start': start, 'end': end, 'color': color, 'weight': weight})
# 更新地图
self.map_obj = folium.Map(location=[0, 0], zoom_start=2)
for path in self.flight_paths:
start = path['start']
end = path['end']
color = path['color']
weight = path['weight']
folium.PolyLine(locations=[start, end], color=color, weight=weight).add_to(self.map_obj)
self.map_obj.fit_bounds([[18.0, 110.0], [35.0, 125.0]])
self.map_obj.zoom_start = 6
self.html = self.map_obj._repr_html_()
self.web.setHtml(self.html)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MapWindow()
window.show()
# 添加最新航迹参数
latest_path = ([25.00, 119.00], [31.23, 121.47], 'purple', 5)
window.add_latest_path(*latest_path)
sys.exit(app.exec_())
```
这个程序使用folium和PyQt5创建一个窗口并在其中嵌入一个WebEngineView对象来显示多个航迹。程序还提供了一个方法`add_latest_path`来添加最新的航迹参数,并去除旧的参数。
在程序运行时,首先会加载多个航迹到地图上,然后调用`add_latest_path`方法添加最新的航迹参数,并更新地图。最终只会显示最新的航迹参数。
您可以根据需要修改航迹数据和方法中的航迹参数来适应您的应用场景。