0

I am trying to get a QGeoLocation. My version of Qt is 5.7.1, btw, and I am running it on Debian.

I saw this post how to get latitude/longitude from one geo address using Qt c++ on windows?

I copied and pasted the working solution from Scheff's answer, but still got not error and 0 locations. Does this have to do with my setup/environment?

This shorter code has the same effect:

#include <QApplication>
#include <QGeoAddress>
#include <QGeoCodingManager>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoServiceProvider>
#include <QtDebug>

int main( int argc, char **argv)
{
    QCoreApplication app( argc, argv );

    QGeoServiceProvider geoSrv( "osm" );
    QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
    QGeoAddress addr;
    addr.setCountry( "China" );
    QGeoCodeReply *geoCode = geoCoder->geocode( addr );

    if ( geoCode->error() )
        qDebug() << "error";

    qDebug() << geoCode->locations().length();

    return app.exec();
}
dspb5
  • 3
  • 2
  • Just a guess. `QGeoAddress addr;` is a local object which goes out of scope when `return app.exec();` is called. Check if `geocode` finished before that happens. – user3606329 May 27 '18 at 03:14
  • Also to output the errors you could simply do something like `QObject::connect(geoCoder, &QGeoCodingManager::error, [=](QGeoCodeReply *reply, QGeoCodeReply::Error error, QString errorString = QString()){ qDebug() << errorString; });` – user3606329 May 27 '18 at 03:21
  • Thanks, that's a good point. How I make addr not go out of scope? – dspb5 May 27 '18 at 15:05
  • @user3606329 I have tried it and there is no error, I have tried with other plugins like esri and I have not had problems but with osm it does not return anything. – eyllanesc May 27 '18 at 18:24
  • @eyllanesc Thanks, I didn't think of changing the plugin. In my code above I replaced "osm" with "esri" and after some debugging got the error message: "The geoservices provider esri is not supported." How did you get the esri plugin? – dspb5 May 27 '18 at 20:26
  • @dspb5 try with: `QGeoServiceProvider geoSrv( "here" )` and use your previous code – eyllanesc May 27 '18 at 20:29
  • @eyllanesc That took a while, had to sign up and get an app_id and token. I am still getting 0 locations, so the problem is probably not the plugin. What code did you use when you tried with esri? – dspb5 May 27 '18 at 21:04
  • dspb5: you can also simply use `QNetworkAccessManager` and consume the result of the API with `QJson`. IIRC `QGeoServiceProvider` does nothing else. – user3606329 May 28 '18 at 14:02
  • `Here` has a great API which is simply to use and can return results in JSON. You can build a request with QNAM and parse the results with QJson and obtain the elements you need. – user3606329 May 28 '18 at 14:16
  • @user3606329 Thanks, I didn't know about that. I may have to do it that way. – dspb5 May 28 '18 at 15:30

2 Answers2

1

I found your post while struggling with the same problem. For me the QGeoServiceProvider code suddenly stopped working with OpenStreetmap. I quickly tried out the "here" api and that seems to work with exactly the same code. With some quick inspection with wireshark I found the problem easily.

QGeoServiceProvider tries to connect to the OpenStreetMap api at this url: http://nominatim.openstreetmap.org where it gets a redirected via a HTTP 303 to https://nominatim.openstreetmap.org. Apparently, the QGeoServiceProvider isn't able to handle this redirection correctly. I fixed it by providing the new url in the osm.geocoding.host parameter. Using your code this would look like this:

#include <QApplication>
#include <QGeoAddress>
#include <QGeoCodingManager>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoServiceProvider>
#include <QtDebug>

int main( int argc, char **argv)
{
   QCoreApplication app( argc, argv );

   //Add this
   QMap<QString,QVariant> params;
   params["osm.geocoding.host"] = "https://nominatim.openstreetmap.org";

   QGeoServiceProvider geoSrv( "osm", params );

   QGeoCodingManager *geoCoder = geoSrv.geocodingManager();
   QGeoAddress addr;
   addr.setCountry( "China" );
   QGeoCodeReply *geoCode = geoCoder->geocode( addr );

   if ( geoCode->error() )
       qDebug() << "error";

   qDebug() << geoCode->locations().length();

   return app.exec();
}

Hope this helps!

tjadejong
  • 26
  • 2
1

Instead of using

QGeoServiceProvider geoSrv( "osm", params );
QGeoCodingManager *geoCoder = geoSrv.geocodingManager();

If you use a pointer instead:

QGeoServiceProvider* geoSrv = new QGeoServiceProvider( "osm", params );
QGeoCodingManager *geoCoder = geoSrv->geocodingManager();

It should work(did for me at least)