How to get latitude/longitude of a geo-address using Qt c++ in windows?

Asked

Viewed 320 times

1

It is possible to get the coordinates (latitude and longitude) from a geographical location using only the address, in Qt c ++?

I know the Qgeocoordinates, Qgeolocation and Qgeoaddress libraries, but I don’t know if it’s possible to get coordinates from an address through them.

All help is welcome.

1 answer

2


Through a reply I received from a user here from Stack Overflow in English, this can be found here, I was able to elaborate a function that can perfectly find the coordinates through the address, follows:

QGeoCoordinate MainWindow::getGeoCoordinates() {
    QEventLoop loop;
    QGeoCoordinate qGeoCoord;
    QStringList qGeoSrvList = QGeoServiceProvider::availableServiceProviders();

    for (QString entry : qGeoSrvList) {
        QGeoServiceProvider qGeoService(entry);
        QGeoCodingManager *pQGeoCoder = qGeoService.geocodingManager();

        if ( ( qGeoService.error() == 0 ) && ( qGeoService.errorString().compare("") == 0 ) ) {
            QLocale qLocaleC(QLocale::C, QLocale::AnyCountry);
            pQGeoCoder->setLocale(qLocaleC);

            QGeoAddress qGeoAddr;
            qGeoAddr.setCountry( "país" );
            qGeoAddr.setPostalCode( "cep" );
            qGeoAddr.setCity( "cidade" );
            qGeoAddr.setStreet( "rua, número" );
            qGeoAddr.setState( "estado" );

            QGeoCodeReply *pQGeoCode = pQGeoCoder->geocode(qGeoAddr);

            connect( pQGeoCode, SIGNAL(finished()), &loop, SLOT(quit()));
            loop.exec();

            if ( pQGeoCode->error() != QGeoCodeReply::NoError ) {
                qDebug() << pQGeoCode->error() << " | " << pQGeoCode->errorString();
                break;
            } else {

                QList<QGeoLocation> qGeoLocs = pQGeoCode->locations();
                for (QGeoLocation &qGeoLoc : qGeoLocs) {
                    qGeoLoc.setAddress(qGeoAddr);
                    qGeoCoord = qGeoLoc.coordinate();
                }
            }
        }
    }

    return qGeoCoord;
}

OBS: Remembering that no . pro should be added QT += Positioning Location

Browser other questions tagged

You are not signed in. Login or sign up in order to post.