geocode() form QGeoCodingManager class not returning anything after passing an address.
-
Hey guys! I feel like I almost have geocode() working but I pass an address to geocode() and it returns an empty list ( longitude gets set to -1 below). Any tips would be greatly appreciated!
#include "mapactions.h"
#include <QGeoPositionInfoSource>
#include <QGeoServiceProvider>
#include <QGeoCodeReply>
#include <QGeoCodingManager>
#include <QGeoAddress>
#include <QGeoCoordinate>
#include <QGeoLocation>
#include <QGeoShape>void MapFunctions::getmyaddress()
{
// QMap<QString,QVariant> params;
// params["osm.geocoding.host"] = "https://nominatim.openstreetmap.org";
QGeoServiceProvider* geoSrv = new QGeoServiceProvider( "osm");
QGeoCodingManager *geoCoder = geoSrv->geocodingManager();
QGeoAddress addr;
addr.setCountry( "Canada" );
addr.setCity("Toronto");
QGeoCodeReply *geoCode = geoCoder->geocode(addr);
if(geoCode->error()!= geoCode->NoError)
{
latitude = 14;
}
if((geoCode->locations()).isEmpty())
{
longitude =-1.0;
return;
}
longitude = (geoCode->locations()).first().coordinate().longitude();
latitude = (geoCode->locations()).first().coordinate().latitude();
} -
Please read the documentation - the call only starts the geocoding. You have to connect to the appropriate signals.
-
This post is deleted!
-
@Christian-Ehrlicher Hey, thanks for the reply! I tried implementing the slot handleFinish() but the values I'm getting are wonky, however, the list is no longer empty! I feel like the issue could be due to me making QGeoCodeReply *geoCode public as I was unsure how to pass it to the slot. Any tips are greatly appreciated!
-
Sorry but this code is neither correct in any way nor does it compile at all. Please learn basic c++ and take a look at the Qt signals/slots examples.
-
@Christian-Ehrlicher Ok, thankyou for your help. I have reviewed my code and reviewed the documentation. The slots and signals are now recognized but I am still not getting reasonable values. The issue is that neither slots are being called. If you could point me in the right direction that would be great.
-
@CaptainJuice Are you sure handleError is not called? How did you check that?
Also, please post your code as text, not screen-shots.
-
@jsulm Sorry about that, will make sure to change that next time. I know it never enters either one of handleError() or handleFinish() because in my QML i have,
Component.onCompleted: {
MapFunctions.getmyaddress();
console.log("Coordinate:", MapFunctions.getlatitude());
console.log("Coordinate:", MapFunctions.getlongitude());
}for the application window. If I set values in those handle() functions they do not get reflected in the console log. For example I set longitude= 14 in handleFinish() and I comment out the other code and this has no effect on the output. For handleError() the values do not get set to -1 so it doesn't get called.