从文本文件接收GPS数据并显示在QT地图中

从文本文件接收GPS数据并显示在QT地图中,qt,qml,Qt,Qml,我正在尝试使用UDP套接字连接通过long和lat接收文本文件数据,收到的数据将显示在我的QT QML映射中。我可以在QT中这样做吗 Plugin { id: mapPlugin name: "osm" } MapQuickItem { id: marker anchorPoint.x: marker.width / 4 anchorPoint.y: marker.height coordinate: QtPo

我正在尝试使用UDP套接字连接通过long和lat接收文本文件数据,收到的数据将显示在我的QT QML映射中。我可以在QT中这样做吗

Plugin {
    id: mapPlugin
    name: "osm" 
   }

   MapQuickItem
   {
      id: marker
      anchorPoint.x: marker.width / 4
      anchorPoint.y: marker.height
      coordinate: QtPositioning.coordinate(13.293470, 135.816885)
      Image { source: "qrc:/marker.png"
             }
        Text { text: "Location" ;font.pointSize: 8; font.bold: true }

       }
    }

必须实现具有position q_属性的QObject,并使用setContextProperty(或使用qmlRegisterType)将其导出到QML,QObject必须具有读取文本每行的方法,将其解析为QGeoCoordination设置作为位置。以下代码是一个示例:

main.cpp

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类MarkerManager:公共QObject
{
Q_对象
Q_属性(QGeoCoordination位置读取位置通知位置更改)
公众:
MarkerManager(QObject*parent=nullptr):QObject(parent){
连接(&timer、&QTimer::timeout、this和&MarkerManager::readLine);
}
bool loadFile(常量QString&filename,int interval=100){
setFileName(文件名);
如果(!file.open(QIODevice::ReadOnly | QIODevice::Text))
返回false;
ts.setDevice(文件(&F);
定时器。启动(间隔);
返回true;
}
QGeoCoordination position()常量{
返回m_位置;
}
Q_信号:
void positionChanged();
专用Q_插槽:
void readLine(){
如果(!ts.atEnd()){
QString-line=ts.readLine();
QStringList elements=line.split(QRegularExpression(\\s+));
if(elements.count()==2){
bool-ok1,ok2;
双lat=元素[0]。toDouble(&ok1);
双lng=元素[1]。TODULE(&ok2);
如果(确定1和确定2){
m_位置=QGeoCoordination(纬度、液化天然气);
Q_EMIT positionChanged();
}
}
}
}
私人:
QFile文件;
qtexts;
定时器;
QGeo坐标m_位置;
};
#包括“main.moc”
int main(int argc,char*argv[])
{
QCoreApplication::setAttribute(Qt::AA_enableHighdDiscaling);
QGUI应用程序应用程序(argc、argv);
市场经理;
加载文件(“data_gps.txt”);
qqmlaplicationengine;
engine.rootContext()->setContextProperty(“管理器”、&manager);
const-QUrl-url(QStringLiteral(“qrc:/main.qml”);
QObject::connect(&engine),&QQmlApplicationEngine::objectCreated,
&应用程序,[url](QObject*obj,const-quorl和objUrl){
如果(!obj&&url==objUrl)
QCoreApplication::退出(-1);
},Qt::QueuedConnection);
引擎加载(url);
返回app.exec();
}
main.qml

import QtQuick 2.12
import QtQuick.Window 2.12

import QtLocation 5.12
import QtPositioning 5.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Plugin {
        id: mapPlugin
        name: "osm"
    }
    Map {
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(14.70202102, 121.0957246)
        zoomLevel: 15
        MapQuickItem{
            id: marker
            anchorPoint.x: marker.width / 4
            anchorPoint.y: marker.height
            coordinate: manager.position
            sourceItem:  Image { source: "qrc:/marker.png"
                Text { text: "Location" ; font.pointSize: 8; font.bold: true }
            }
        }
    }
}
可以找到完整的示例

您必须实现具有position q_属性的QObject,并使用setContextProperty(或使用qmlRegisterType)将其导出到QML,该QObject必须具有读取文本每行的方法,将其解析为QGeoCoordination设置作为位置。以下代码是一个示例:

main.cpp

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类MarkerManager:公共QObject
{
Q_对象
Q_属性(QGeoCoordination位置读取位置通知位置更改)
公众:
MarkerManager(QObject*parent=nullptr):QObject(parent){
连接(&timer、&QTimer::timeout、this和&MarkerManager::readLine);
}
bool loadFile(常量QString&filename,int interval=100){
setFileName(文件名);
如果(!file.open(QIODevice::ReadOnly | QIODevice::Text))
返回false;
ts.setDevice(文件(&F);
定时器。启动(间隔);
返回true;
}
QGeoCoordination position()常量{
返回m_位置;
}
Q_信号:
void positionChanged();
专用Q_插槽:
void readLine(){
如果(!ts.atEnd()){
QString-line=ts.readLine();
QStringList elements=line.split(QRegularExpression(\\s+));
if(elements.count()==2){
bool-ok1,ok2;
双lat=元素[0]。toDouble(&ok1);
双lng=元素[1]。TODULE(&ok2);
如果(确定1和确定2){
m_位置=QGeoCoordination(纬度、液化天然气);
Q_EMIT positionChanged();
}
}
}
}
私人:
QFile文件;
qtexts;
定时器;
QGeo坐标m_位置;
};
#包括“main.moc”
int main(int argc,char*argv[])
{
QCoreApplication::setAttribute(Qt::AA_enableHighdDiscaling);
QGUI应用程序应用程序(argc、argv);
市场经理;
加载文件(“data_gps.txt”);
qqmlaplicationengine;
engine.rootContext()->setContextProperty(“管理器”、&manager);
const-QUrl-url(QStringLiteral(“qrc:/main.qml”);
QObject::connect(&engine),&QQmlApplicationEngine::objectCreated,
&应用程序,[url](QObject*obj,const-quorl和objUrl){
如果(!obj&&url==objUrl)
QCoreApplication::退出(-1);
},Qt::QueuedConnection);
引擎加载(url);
返回app.exec();
}
main.qml

import QtQuick 2.12
import QtQuick.Window 2.12

import QtLocation 5.12
import QtPositioning 5.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Plugin {
        id: mapPlugin
        name: "osm"
    }
    Map {
        id: map
        anchors.fill: parent
        plugin: mapPlugin
        center: QtPositioning.coordinate(14.70202102, 121.0957246)
        zoomLevel: 15
        MapQuickItem{
            id: marker
            anchorPoint.x: marker.width / 4
            anchorPoint.y: marker.height
            coordinate: manager.position
            sourceItem:  Image { source: "qrc:/marker.png"
                Text { text: "Location" ; font.pointSize: 8; font.bold: true }
            }
        }
    }
}
可以找到完整的示例

让我们来看看。现在,我将.txt文件更改为.csv。我现在正在接收数据,但我的问题是如何在地图中显示收到的数据。您还没有回答我的问题:地图显示时是否修改了.txt或.csv文件?地图显示时未修改.csv和.txt文件。我收到了.csv和.txt文件,但我的问题是如何在qt qml mapLet中显示数据表单.csv或.txt文件现在,我将.txt文件更改为.csv。我现在正在接收数据,但我的问题是如何在地图中显示接收到的数据。您尚未回答我的问题:地图显示时是否修改了.txt或.csv文件?地图显示时是否修改了.csv和.txt文件?我收到了.csv和.txt文件,但我的问题是如何显示数据表单.csv或