Qt:移动地理定位适用于模拟器,但不适用于 N900
我使用以下代码为我的应用程序获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该从
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 torequestUpdate()
also triggers thepositionUpdated()
signal. If it does not get an update after your timeout setting, it will signalrequestTimeout()
which you might want to connect a slot to for informational purposes. It is likely that when you retrievelastKnownPosition()
, 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 thatrequestUpdate()
will always return immediately, not after it has successfully received an update so you should callsource->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?