【发布时间】:2017-06-02 04:54:59
【问题描述】:
我正在使用 QT 为 Android、iOS、Windows 开发跨平台应用程序。我想获取设备当前位置。我正在使用Qt Positioning Api。我已经使用Samples available 编写了 C++ 代码。应用程序希望以指定的时间间隔(x 秒)或指定的距离(y 米)以米为单位更新设备位置。请找到下面编写的示例代码。代码导致大量电池消耗,如果网络关闭,几乎整个电池寿命的 9% 也需要大量时间来更新第一个位置。由于要求以指定的时间间隔连续更新设备位置,无法停止位置更新 @987654323 @
Locationhandler.h
#ifndef LOCATIONHANDLER_H
#define LOCATIONHANDLER_H
#include <QObject>
#include <QGeoPositionInfo>
#include <QGeoPositionInfoSource>
#include <QDebug>
#include "main.h"
class LocationHandler : public QObject
{
Q_OBJECT
public :
explicit LocationHandler(QObject *parent = 0);
QGeoCoordinate getCurrentLocation();
private :
QGeoCoordinate currentLocation;
QGeoPositionInfoSource *source;
signals :
public slots :
void positionUpdated(const QGeoPositionInfo &info);
};
#endif // LOCATIONHANDLER_H
locationhandler.cpp
#include "LocationHandler.h"
LocationHandler::LocationHandler(QObject *parent) : QObject(parent)
{
source = QGeoPositionInfoSource::createDefaultSource(this);
if (source) {
connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)),
this, SLOT(positionUpdated(QGeoPositionInfo)));
source->startUpdates();
source->setUpdateInterval(60);
}
}
void LocationHandler::positionUpdated(const QGeoPositionInfo &info)
{
if(info.isValid())
{
currentLocation = info.coordinate();
qDebug() << "Current Latitude : " << currentLocation.latitude();
qDebug() << "Current Longitude : " << currentLocation.longitude();
updateDeviceCordinate(currentLocation);
}
}
请告诉我,
- 有什么方法可以指定 GPS 更新间隔 时间和距离方面可以减少 CPU 活动时间。
- 另外我想知道有没有什么直接的方法可以让我获得设备 直接使用 QT API 定位,没有任何(QT 信号)回调 快速发挥作用。
注意:基于 qml 的位置更新可能没有帮助,因为我们想在 C++ 代码中实现功能。
【问题讨论】: