如何在PyQt5 Python中使用Folium创建实时GPS跟踪器

brqmpdu1  于 2023-05-02  发布在  Python
关注(0)|答案(1)|浏览(338)

我正在做一个项目,我有一个gpx文件,其中包含latitudelongitude
我需要在pyqt5窗口中实时绘制一条线,这意味着它应该加载窗口,然后继续绘制线。
这将在以后被来自车辆的实时gps数据所取代,这样我们就可以标出车辆的移动位置。
到目前为止,使用foliumgpxpy我已经能够绘制这条线,但我无法找到任何有用的资源来解释如何实时做到这一点。
下面是代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt5.QtWebEngineWidgets import QWebEngineView
import folium
import gpxpy

class MapWidget(QWidget):
    def __init__(self, parent=None):
        super(MapWidget, self).__init__(parent)

        # Parse the GPX file
        gpx_file = open('Init.gpx', 'r')
        gpx = gpxpy.parse(gpx_file)

        # Create a Folium map object
        m = folium.Map(location=[51.44948037, -0.47322678], zoom_start=25)

        # Add the GPX track to the map
        for track in gpx.tracks:
            for segment in track.segments:
                folium.PolyLine([(p.latitude, p.longitude) for p in segment.points], color='red', weight=2.5, opacity=1).add_to(m)

        # Create a QWebEngineView object to display the map
        self.webview = QWebEngineView()
        self.webview.setHtml(m._repr_html_())

        # Create a layout and add the webview to it
        layout = QVBoxLayout(self)
        layout.addWidget(self.webview)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MapWidget()
    widget.show()
    sys.exit(app.exec_())

输出;

谁能推荐一个允许实时数据刷新的可能实现?

cidc1ykv

cidc1ykv1#

我需要在pyqt5窗口中实时绘制一条线,这意味着它应该加载窗口,然后继续绘制线
在PyQt中,这需要一个QtCore/QTimer,如“QTimer example for PyQt5”所示:
如果在应用程序中周期性地执行操作,例如周期性地检测主机的CPU值,则需要QTimer定时器。
当窗口控件收到超时信号时,它将停止此计时器。
在您的原始代码中,使用folium.PolyLine...add_to(m)绘制线条。
相反,您可以从空折线开始:

self.polyline = folium.PolyLine([], color='red', weight=2.5, opacity=1).add_to(self.m)

然后使用QTimer循环添加GPX数据(或最终的实时GPS数据)中的行

# Create a QTimer object to update the polyline periodically
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.add_point_to_polyline)
        self.timer.start(1000)  # Update every 1000 milliseconds (1 second)

add_point_to_polyline方法将通过更新folium.PolyLine对象的locations属性来绘制线条,该属性存储为self.polyline

def add_point_to_polyline(self):
        try:
            point = next(self.points_iterator)
            self.polyline_points.append(point)
            self.polyline.locations = self.polyline_points
            self.webview.setHtml(self.m._repr_html_())
        except StopIteration:
            self.timer.stop()

把这些放在一起:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QTimer
import folium
import gpxpy

class MapWidget(QWidget):
    def __init__(self, parent=None):
        super(MapWidget, self).__init__(parent)

        # Parse the GPX file
        gpx_file = open('Init.gpx', 'r')
        gpx = gpxpy.parse(gpx_file)
        
        self.points_iterator = iter([(p.latitude, p.longitude) for track in gpx.tracks for segment in track.segments for p in segment.points])
        self.polyline_points = []

        # Create a Folium map object
        self.m = folium.Map(location=[51.44948037, -0.47322678], zoom_start=25)

        # Add an empty polyline to the map
        self.polyline = folium.PolyLine([], color='red', weight=2.5, opacity=1).add_to(self.m)

        # Create a QWebEngineView object to display the map
        self.webview = QWebEngineView()
        self.webview.setHtml(self.m._repr_html_())

        # Create a layout and add the webview to it
        layout = QVBoxLayout(self)
        layout.addWidget(self.webview)

        # Create a QTimer object to update the polyline periodically
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.add_point_to_polyline)
        self.timer.start(1000)  # Update every 1000 milliseconds (1 second)

    def add_point_to_polyline(self):
        try:
            point = next(self.points_iterator)
            self.polyline_points.append(point)
            self.polyline.locations = self.polyline_points
            self.webview.setHtml(self.m._repr_html_())
        except StopIteration:
            self.timer.stop()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = MapWidget()
    widget.show()
    sys.exit(app.exec_())

相关问题