Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't get location from geocode()
Forum Updated to NodeBB v4.3 + New Features

Can't get location from geocode()

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 296 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • msauer751M Offline
    msauer751M Offline
    msauer751
    wrote on last edited by
    #1

    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

    Pl45m4P 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which version of Qt ?
      On which OS ?
      Can you make your sample a complete minimal compilable example ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • msauer751M Offline
        msauer751M Offline
        msauer751
        wrote on last edited by
        #3

        Hi,
        Qt 6.9.0,
        Linux Kubuntu 25.04

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • msauer751M msauer751

            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

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @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.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            JonBJ 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Pl45m4 , @msauer751
              I have never used an old-style connection, but if it is "wrong" as you suggest doesn't the connect() return a testable error at runtime for the OP to check per https://doc.qt.io/qt-6/qobject.html#connect

              The 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.

              1 Reply Last reply
              0
              • msauer751M Offline
                msauer751M Offline
                msauer751
                wrote on last edited by
                #7

                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.

                Pl45m4P 1 Reply Last reply
                0
                • msauer751M msauer751

                  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.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #8

                  @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);
                  

                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  JonBJ 1 Reply Last reply
                  1
                  • Pl45m4P Pl45m4

                    @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);
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @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 the connect()s, are you allowing the Qt event loop to run so that signals can be delivered?

                    Pl45m4P 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @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 the connect()s, are you allowing the Qt event loop to run so that signals can be delivered?

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #10

                      @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 the QGeoCodeReply::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.


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      0
                      • msauer751M Offline
                        msauer751M Offline
                        msauer751
                        wrote on last edited by
                        #11

                        Hi,
                        I found the problem. I defined

                            QGeoServiceProvider geoServiceProvider;
                            QGeoCodingManager *geoCodingManager {nullptr};
                        

                        in the local function. After I moved geoServiceProvider and geoCodingManager in the class definition as private members, the the slots will executed.

                        Christian EhrlicherC 1 Reply Last reply
                        0
                        • msauer751M msauer751

                          Hi,
                          I found the problem. I defined

                              QGeoServiceProvider geoServiceProvider;
                              QGeoCodingManager *geoCodingManager {nullptr};
                          

                          in the local function. After I moved geoServiceProvider and geoCodingManager in the class definition as private members, the the slots will executed.

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @msauer751 That's why we want a minimal, compilable example instead just some code pieces... it would have saved you two weeks.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved