Qt:移动地理定位适用于模拟器,但不适用于 N900

发布于 2024-09-24 00:07:34 字数 878 浏览 4 评论 0原文

我使用以下代码为我的应用程序获取 GeoLocation

QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this);
if (source) {
    source->setUpdateInterval(1000); // time in milliseconds
    source->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);
    connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
    source->startUpdates();
    source->requestUpdate(30000);
    const QGeoPositionInfo &info =source->lastKnownPosition();            
    ui->label->setText(QString("Latitude - %1  Longitude - %2").arg(info.coordinate().latitude()).arg(info.coordinate().longitude()));
}

此代码在模拟器上完美运行,并为我提供模拟器提供的坐标,但是当我尝试在 N900 上运行它时,这不起作用。它返回 Nan 而不是纬度和经度坐标。目前手机上的 GPS 信号精度较低。手机上的 OVI 地图应用程序也可以进行地理定位。知道为什么上面的代码无法在手机上获取地理位置但在模拟器上运行良好吗?

I use the following code to get GeoLocation for my app

QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this);
if (source) {
    source->setUpdateInterval(1000); // time in milliseconds
    source->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);
    connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdated(QGeoPositionInfo)));
    source->startUpdates();
    source->requestUpdate(30000);
    const QGeoPositionInfo &info =source->lastKnownPosition();            
    ui->label->setText(QString("Latitude - %1  Longitude - %2").arg(info.coordinate().latitude()).arg(info.coordinate().longitude()));
}

This code runs perfectly on the Simulator and gives me the coordinates provided by the simulator however this does not work when i try to run it on my N900. It returns Nan instead of the latitude and longitude coordinate. The current GPS signal on the phone is coarse accuracy. Also geolocation is working in the OVI Maps app on the phone. Any idea why the above code is unable to get the geolocation on the phone but works perfectly on the simulator ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾家有女初长成 2024-10-01 00:07:34

您应该从 positionUpdated() 的槽中设置标签,因为收到更新时会触发信号。您对 requestUpdate() 的调用也会触发 positionUpdated() 信号。如果它在超时设置后没有得到更新,它将向您发出 requestTimeout() 信号,您可能希望将插槽连接到该信号以供参考。当您检索 lastKnownPosition() 时,可能尚未确定位置并且返回的值为空值。仅从文档中很难确定,但我认为这意味着 requestUpdate() 将始终立即返回,而不是在成功接收更新后返回,因此您应该调用 source->lastKnownPosition .isValid() 查看它是否包含一个好的位置。

您确实应该检查 positionUpdated() 插槽中的位置,或者在该插槽至少被调用一次之后。模拟器很可能有一个立即可用的位置并且在这种情况下可以工作。

您的 positionUpdated() 插槽是否被调用过?

You should be setting the label from the slot for positionUpdated() as the signal is fired when an update has been received. Your call to requestUpdate() also triggers the positionUpdated() signal. If it does not get an update after your timeout setting, it will signal requestTimeout() which you might want to connect a slot to for informational purposes. It is likely that when you retrieve lastKnownPosition(), no position has been determined yet and the value returned is a null value. It's difficult to be sure just from the documentation but I think it implies that requestUpdate() will always return immediately, not after it has successfully received an update so you should call source->lastKnownPosition.isValid() to see if it contains a good position.

You should really be checking the position in the positionUpdated() slot or after that slot has been called at least once. It's likely the simulator has a postion available immediately and works in that case.

Does your positionUpdated() slot ever get called?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文