如何在qt移动性中通过gps数据计算速度?

时间:2011-07-04 15:03:36

标签: qt location qt-mobility

如何通过在qt中使用gps数据来获得我的速度和距离? 我有这样的课,也许是任何标准的方法?

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "quitdiallog.h"
#include <QGeoCoordinate>
#include <QDebug>
#include <QtGui/QMessageBox>
#include <QList>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    setWindowTitle("Мой кОмпаС");
    ui->setupUi(this);
    startGPS();

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::startGPS()
{
    // Obtain the location data source if it is not obtained already
    if (!locationDataSource)
    {
        locationDataSource =
                QGeoPositionInfoSource::createDefaultSource(this);
        if (locationDataSource)
        {
            // Whenever the location data source signals that the current
            // position is updated, the positionUpdated function is called.
            QObject::connect(locationDataSource,
                             SIGNAL(positionUpdated(QGeoPositionInfo)),
                             this,
                             SLOT(positionUpdated(QGeoPositionInfo)));
            // Start listening for position updates
                    locationDataSource->setUpdateInterval(200);
            locationDataSource->startUpdates();
        } else {
            // Not able to obtain the location data source
            // TODO: Error handling
        }
    } else {
        // Start listening for position updates
        locationDataSource->setUpdateInterval(5000);
        locationDataSource->startUpdates();
    }
}

void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)
{
    if (geoPositionInfo.isValid())
    {
        //locationDataSource->stopUpdates();
        QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
        this->latitude = geoCoordinate.latitude();
        this->longitude = geoCoordinate.longitude();
        this->altitude=geoCoordinate.altitude();
    ui->label->setNum(latitude);
    ui->label_2->setNum(longitude);
    }
}



void MainWindow::on_pushButton_clicked()
{
    ui->label_3->setNum(latitude);
    qDebug()<<latitude<<"    "<<longitude<<"   "<<altitude;
}

void MainWindow::on_pushButton_4_clicked()
{
    QuitDiallog *qi=new QuitDiallog;
    this->hide();
    qi->show();
}

如何获得速度???

1 个答案:

答案 0 :(得分:2)

首先我会做source->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);

地面速度可以通过geoPositionInfo.attribute(QGeoPositionInfo::GroundSpeed)

获取(理论上)

但请确保geoPositionInfo.hasAttribute(QGeoPositionInfo::GroundSpeed) == true AFAIK在一些移动设备上存在一些地面问题。