How it works…

I won't be able to tell you exactly how Google obtains the address from its backend system, but I can teach you how to request the data from Google by using QNetworkRequest. All you need to do is to set the URL of the network request to the URL I used in the previous source code and append both the latitude and longitude information to the URL.

After that, all we can do is wait for the response from the Google API server. We need to specify XML as the desired format when sending the request to Google; otherwise, it may return the results in JSON format. This can be done by adding the xml keyword within the network request URL, as highlighted here:

request.setUrl(QUrl("http://maps.googleapis.com/maps/api/geocode/xml?keylatlng=" + latitude + "," + longitude + "&key=AIzaSyBhKayXIr2zgMW2olsxtuZ7x2QWyLo1itQ"));

When the program receives the response from Google, the getAddressFinished() slot function will be called and we will be able to obtain the data sent by Google through QNetworkReply.

Google usually replies with a long text in XML format, which contains a ton of data we don't need. We used the QXmlStreamReader class to parse the data because in this case, we don't have to care about the parent-child relationship of the XML structure.

All we need is the text stored in the formatted_address element in the XML data. Since there is more than one element by the name of formatted_address, we just need to find the first one and ignore the rest. You can also do the reverse by providing an address to Google and obtain the location's coordinate from its network response.