qt c++ fonction converting adress to coordinates (longitude, latitude)

26 views Asked by At

I am working on a project with c++ in qt I am working on a function that convert an adress to coordinates ( i'am using openstreetmap) so I can calculate distance between two adresses does it need specified format or something

it always displays

"No coordinates found for the address

i am trying to get a solution for my problem and i hope it gets resolved

void MainWindow::geocodeAndPrint(QString address) {
    qDebug() << "Geocoding address:" << address;

    // Set up the QGeoServiceProvider to use the OpenStreetMap provider
    QGeoServiceProvider provider("osm");
    QGeoCodingManager *geoCodingManager = provider.geocodingManager();

    // Create a QGeoAddress object
    QGeoAddress geoAddress;
    geoAddress.setText(address);

    // Perform geocoding for the address
    QGeoCodeReply *reply = geoCodingManager->geocode(geoAddress);

    // Create local event loop to wait for reply
    QEventLoop loop;
    QObject::connect(reply, &QGeoCodeReply::finished, &loop, &QEventLoop::quit);
    loop.exec(); // Wait here until finished signal is emitted

    // Handle the reply
    qDebug() << "Geocoding finished";
    if (reply->error() == QGeoCodeReply::NoError) {
        const QList<QGeoLocation> locations = reply->locations();
        if (!locations.isEmpty()) {
            for (const QGeoLocation &location : locations) {
                const QGeoCoordinate coordinate = location.coordinate();
                qDebug() << "Latitude:" << coordinate.latitude() << "Longitude:" << coordinate.longitude();
            }
        } else {
            qDebug() << "No coordinates found for the address:" << address;
        }
    } else {
        qDebug() << "Geocoding error:" << reply->errorString();
    }

    reply->deleteLater();
    qDebug() << "Geocoding request sent";
}

void MainWindow::testGeocodeAndPrint() {
    // Call the function with test data
    geocodeAndPrint("1600 Amphitheatre Parkway, Mountain View, CA, USA");

    // Wait for the asynchronous operation to finish (if needed)
    QCoreApplication::processEvents();
}
0

There are 0 answers