Can't get location from geocode()
-
Hi,
I want to get the latitude and longitude from an adresse. So I do the following:QGeoServiceProvider geoServiceProvider("osm"); QGeoCodingManager *geoCodingManager {geoServiceProvider.geocodingManager()}; QLocale localeC(QLocale::German, QLocale::Germany); geoCodingManager->setLocale(localeC); // build address QGeoAddress geoAddr; geoAddr.setCountry(temp.land); geoAddr.setPostalCode(temp.plz); geoAddr.setCity(temp.ort); geoAddr.setStreet(temp.adresse); geoCode = geoCodingManager->geocode(geoAddr); QObject::connect(geoCode, SIGNAL(finished()), this, SLOT(getlonlat())); QObject::connect(geoCode, SIGNAL(aborted()), this, SLOT(getAbort())); QObject::connect(geoCode, &QGeoCodeReply::errorOccurred, this, &Winzer::getError);
and geoCode is global QGeoCodeReply
SLOT(getLonLat()):
void Winzer::getlonlat() { QObject::disconnect(geoCode, SIGNAL(finished()), nullptr, nullptr); QList<QGeoLocation> geoLocs {geoCode->locations()}; qDebug() << ">> Winzer > getlonlat (" << __LINE__ << ") \t geoLocs = " << geoLocs.size(); for (QGeoLocation &geoLoc : geoLocs) { QGeoCoordinate geoCoord {geoLoc.coordinate()}; qDebug() << ">> Winzer > getlonlat (" << __LINE__ << ") \t geoLoc = " << geoLoc.coordinate(); } }
SLOT(getError):
void Winzer::getError(QGeoCodeReply::Error errorNumber, const QString &errorString) { QObject::disconnect(geoCode, SIGNAL(errorOccurred()), nullptr, nullptr); qDebug() << ">> Winzer > getError (" << __LINE__ << ") \t errorNumber = " << errorNumber << "< errorString= " << errorString; }
But the slots are never activated.
Can you tell me, where do the mistak?
Thank you for your help.
BR
martin -
Hi,
Which version of Qt ?
On which OS ?
Can you make your sample a complete minimal compilable example ? -
Thanks
Just missing the last point: make your example fully compilable. That way people will be able to test/debug help you without having to rewrite your code. -
Hi,
I want to get the latitude and longitude from an adresse. So I do the following:QGeoServiceProvider geoServiceProvider("osm"); QGeoCodingManager *geoCodingManager {geoServiceProvider.geocodingManager()}; QLocale localeC(QLocale::German, QLocale::Germany); geoCodingManager->setLocale(localeC); // build address QGeoAddress geoAddr; geoAddr.setCountry(temp.land); geoAddr.setPostalCode(temp.plz); geoAddr.setCity(temp.ort); geoAddr.setStreet(temp.adresse); geoCode = geoCodingManager->geocode(geoAddr); QObject::connect(geoCode, SIGNAL(finished()), this, SLOT(getlonlat())); QObject::connect(geoCode, SIGNAL(aborted()), this, SLOT(getAbort())); QObject::connect(geoCode, &QGeoCodeReply::errorOccurred, this, &Winzer::getError);
and geoCode is global QGeoCodeReply
SLOT(getLonLat()):
void Winzer::getlonlat() { QObject::disconnect(geoCode, SIGNAL(finished()), nullptr, nullptr); QList<QGeoLocation> geoLocs {geoCode->locations()}; qDebug() << ">> Winzer > getlonlat (" << __LINE__ << ") \t geoLocs = " << geoLocs.size(); for (QGeoLocation &geoLoc : geoLocs) { QGeoCoordinate geoCoord {geoLoc.coordinate()}; qDebug() << ">> Winzer > getlonlat (" << __LINE__ << ") \t geoLoc = " << geoLoc.coordinate(); } }
SLOT(getError):
void Winzer::getError(QGeoCodeReply::Error errorNumber, const QString &errorString) { QObject::disconnect(geoCode, SIGNAL(errorOccurred()), nullptr, nullptr); qDebug() << ">> Winzer > getError (" << __LINE__ << ") \t errorNumber = " << errorNumber << "< errorString= " << errorString; }
But the slots are never activated.
Can you tell me, where do the mistak?
Thank you for your help.
BR
martin@msauer751 said in Can't get location from geocode():
But the slots are never activated.
Because your connections are wrong, I would say.
Don't use the old style and see if this even compiles after changing to functors.@msauer751 said in Can't get location from geocode():
QObject::connect(geoCode, SIGNAL(finished()), this, SLOT(getlonlat()));
Speaking of this one for example.
-
@msauer751 said in Can't get location from geocode():
But the slots are never activated.
Because your connections are wrong, I would say.
Don't use the old style and see if this even compiles after changing to functors.@msauer751 said in Can't get location from geocode():
QObject::connect(geoCode, SIGNAL(finished()), this, SLOT(getlonlat()));
Speaking of this one for example.
@Pl45m4 , @msauer751
I have never used an old-style connection, but if it is "wrong" as you suggest doesn't theconnect()
return a testable error at runtime for the OP to check per https://doc.qt.io/qt-6/qobject.html#connectThe function returns a QMetaObject::Connection that represents a handle to a connection if it successfully connects the signal to the slot. The connection handle will be invalid if it cannot create the connection, for example, if QObject is unable to verify the existence of either signal or method, or if their signatures aren't compatible. You can check if the handle is valid by casting it to a bool.
-
Ok, I changed QObject::connect to the new style:
QObject::connect(geoCode, &QGeoCodeReply::finished, this, &Winzer::getlonlat); QObject::connect(geoCode, &QGeoCodeReply::aborted, this, &Winzer::getAbort); QObject::connect(geoCode, &QGeoCodeReply::errorOccurred, this, &Winzer::getError);
But the result is the same. The slot will never called.
-
Ok, I changed QObject::connect to the new style:
QObject::connect(geoCode, &QGeoCodeReply::finished, this, &Winzer::getlonlat); QObject::connect(geoCode, &QGeoCodeReply::aborted, this, &Winzer::getAbort); QObject::connect(geoCode, &QGeoCodeReply::errorOccurred, this, &Winzer::getError);
But the result is the same. The slot will never called.
@msauer751 said in Can't get location from geocode():
But the result is the same. The slot will never called.
I've tried your code (modified only to make it compile) and it works for me...
So there is something else wrong with your code or class structure.
Where do you run this code? Where it is written?
Are all objects valid until the signal is emitted?#include "mainwindow.h" #include <QApplication> #include <QtLocation/QGeoServiceProvider> #include <QtLocation/QGeoCodingManager> #include <QtPositioning/QGeoAddress> #include <QtPositioning/QGeoLocation> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; QGeoServiceProvider geoServiceProvider("osm"); QGeoCodingManager *geoCodingManager {geoServiceProvider.geocodingManager()}; QLocale localeC(QLocale::German, QLocale::Germany); geoCodingManager->setLocale(localeC); // Address of Qt Company HQ in Berlin QGeoAddress geoAddr; geoAddr.setCountry("Germany"); geoAddr.setPostalCode("12489"); geoAddr.setCity("Berlin"); geoAddr.setStreet("Erich-Thilo-Straße 10"); QGeoCodeReply* geoCode = geoCodingManager->geocode(geoAddr); QObject::connect(geoCode, &QGeoCodeReply::finished, [geoCode](){ qDebug() << "finished"; QList<QGeoLocation> geoLocs {geoCode->locations()}; qDebug() << ">> geoLocs = " << geoLocs.size(); for (QGeoLocation &geoLoc : geoLocs) { QGeoCoordinate geoCoord {geoLoc.coordinate()}; qDebug() << ">> geoLoc = " << geoLoc.coordinate(); } }); QObject::connect(geoCode, &QGeoCodeReply::aborted, [](){ qDebug() << "aborted"; }); QObject::connect(geoCode, &QGeoCodeReply::errorOccurred, [&](QGeoCodeReply::Error error, const QString &errorString = QString()){ qDebug() << errorString << error; }); w.show(); return a.exec(); }
This is the code I used with an empty
QMainWindow
And this is my reply:
finished >> geoLocs = 1 >> geoLoc = QGeoCoordinate(52.4318192, 13.5330535)
which matches the (Lat., Long.) of Berlin and that address in specific.
BTW:
Instead of disconnecting the function after the first call, you can use
Qt::SingleShotConnection
QObject::connect(geoCode, &QGeoCodeReply::finished, this, &Winzer::getlonlat, Qt::SingleShotConnection);
-
@msauer751 said in Can't get location from geocode():
But the result is the same. The slot will never called.
I've tried your code (modified only to make it compile) and it works for me...
So there is something else wrong with your code or class structure.
Where do you run this code? Where it is written?
Are all objects valid until the signal is emitted?#include "mainwindow.h" #include <QApplication> #include <QtLocation/QGeoServiceProvider> #include <QtLocation/QGeoCodingManager> #include <QtPositioning/QGeoAddress> #include <QtPositioning/QGeoLocation> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; QGeoServiceProvider geoServiceProvider("osm"); QGeoCodingManager *geoCodingManager {geoServiceProvider.geocodingManager()}; QLocale localeC(QLocale::German, QLocale::Germany); geoCodingManager->setLocale(localeC); // Address of Qt Company HQ in Berlin QGeoAddress geoAddr; geoAddr.setCountry("Germany"); geoAddr.setPostalCode("12489"); geoAddr.setCity("Berlin"); geoAddr.setStreet("Erich-Thilo-Straße 10"); QGeoCodeReply* geoCode = geoCodingManager->geocode(geoAddr); QObject::connect(geoCode, &QGeoCodeReply::finished, [geoCode](){ qDebug() << "finished"; QList<QGeoLocation> geoLocs {geoCode->locations()}; qDebug() << ">> geoLocs = " << geoLocs.size(); for (QGeoLocation &geoLoc : geoLocs) { QGeoCoordinate geoCoord {geoLoc.coordinate()}; qDebug() << ">> geoLoc = " << geoLoc.coordinate(); } }); QObject::connect(geoCode, &QGeoCodeReply::aborted, [](){ qDebug() << "aborted"; }); QObject::connect(geoCode, &QGeoCodeReply::errorOccurred, [&](QGeoCodeReply::Error error, const QString &errorString = QString()){ qDebug() << errorString << error; }); w.show(); return a.exec(); }
This is the code I used with an empty
QMainWindow
And this is my reply:
finished >> geoLocs = 1 >> geoLoc = QGeoCoordinate(52.4318192, 13.5330535)
which matches the (Lat., Long.) of Berlin and that address in specific.
BTW:
Instead of disconnecting the function after the first call, you can use
Qt::SingleShotConnection
QObject::connect(geoCode, &QGeoCodeReply::finished, this, &Winzer::getlonlat, Qt::SingleShotConnection);
@Pl45m4
I know nothing about this, but could OP simply not be getting any "response" to "request" sent? That would explain no slots called.@msauer751
After you have set up theconnect()
s, are you allowing the Qt event loop to run so that signals can be delivered? -
@Pl45m4
I know nothing about this, but could OP simply not be getting any "response" to "request" sent? That would explain no slots called.@msauer751
After you have set up theconnect()
s, are you allowing the Qt event loop to run so that signals can be delivered?@JonB said in Can't get location from geocode():
could OP simply not be getting any "response" to "request" sent?
I don't know in what situation none of the three connected signals are fired... so I guess no.
I also tried with invalid address data (city = "khjgfkg" and street = "bvxycvcj") and I still get theQGeoCodeReply::finished
signal. With no valid lat/long but that doesn't matter in this case.Therefore I guess it's either mine "object lifetime" issue or your "blocked event loop". However for the latter I don't see any reason for this to happen, assuming OP's
Winzer
widget/object is working properly.So @msauer751 needs to check the code that is not visible to us for Qt design and C++ issues... and probably find the cause of this problem.