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

1

Is it possible to get the coordinates (latitude and longitude) of a geographic location using only the address in Qt c ++?

I know the libraries QGeoCoordinates, QGeoLocation and QGeoAddress, but I do not know if it is possible to get coordinates of an address through them.

All help is welcome.

    
asked by anonymous 06.04.2017 / 17:34

1 answer

2

Through a response I received from a user here from Stack Overflow in English, this can be found here , I could elaborate a function that perfectly can find the coordinates through the address,

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;
}

NOTE: Remembering that .pro should be added QT + = positioning location

    
18.04.2017 / 14:23