views:

92

answers:

1

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 ?

A: 

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?

Arnold Spence