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();
}
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!