How to do it…

Let's create a program that utilizes the Geocoding API by following these steps:

  1. Create a new Qt Widgets Application project.
  2. Open mainwindow.ui and add a couple of text labels, input fields, and a button to make your UI to look similar to this:

  1. Open your project (.pro) file and add the network module to your project. You can do that by simply adding the network text after core and gui, as shown in the following code:
QT += core gui network
  1. Open mainwindow.h and add the following headers to the source code:
#include <QMainWindow>:
#include <QDebug>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QXmlStreamReader>
  1. Declare a slot function manually and call it getAddressFinished():
private slots:
void getAddressFinished(QNetworkReply* reply);
  1. Declare a private variable called addressRequest:
private:
QNetworkAccessManager* addressRequest;
  1. Open mainwindow.ui again, right-click on the Get Address button, and select Go to slot…. Then choose the clicked() option and press OK. A slot function will now be added to both the mainwindow.h and mainwindow.cpp source files.
  2. Open mainwindow.cpp and add the following code to the class constructor:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);
addressRequest = new QNetworkAccessManager();
connect(addressRequest, &QNetworkAccessManager::finished, this, &MainWindow::getAddressFinished);
}
  1. Add the following code to the getAddressFinished() slot function we just declared manually:
void MainWindow::getAddressFinished(QNetworkReply* reply) {
QByteArray bytes = reply->readAll();
//qDebug() << QString::fromUtf8(bytes.data(),
bytes.size());
QXmlStreamReader xml;
xml.addData(bytes);
  1. Continue to loop through the XML file to obtain the text element:
    while(!xml.atEnd()) {
if (xml.isStartElement()) {
QString name = xml.name().toString();
//qDebug() << name;
if (name == "formatted_address") {
QString text = xml.readElementText();
qDebug() << "Address:" << text;
return;
}
}
xml.readNext();
}
  1. Check for any error text:
    if (xml.hasError()) {
qDebug() << "Error loading XML:" <<
xml.errorString();
return;
}
qDebug() << "No result.";
}
  1. Add the following code to the clicked() slot function created by Qt:
void MainWindow::on_getAddressButton_clicked() {
QString latitude = ui->latitude->text();
QString longitude = ui->longitude->text();
QNetworkRequest request;
request.setUrl(QUrl("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&key=AIzaSyBhKayXIr2zgMW2olsxtuZ7x2QWyLo1itQ"));
addressRequest->get(request);
}
  1. Build and run the program and you should be able to obtain the address by inserting the longitude and latitude values and clicking the Get Address button:

  1. Let's try with a longitude of -73.9780838 and a latitude of 40.6712957. Click the Get Address button and you will see the following result in the application output window:
Address: "180-190 7th Ave, Brooklyn, NY 11215, USA"
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset