Fetching a map tile in Qt using Google Maps
This code snippet demonstrates how to fetch a map tile in Qt using the Location module of Qt Mobility and Google Maps. It is assumed here that you have set up Qt Mobility in your development environment and on your device. For more information, see CS001615 - Setting up Qt Mobility.
Article Metadata
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition, Maemo 5
Article
Created: tapla
(25 May 2010)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Qt project file
Add the networking features as well as the Location module into the project:
QT += network
CONFIG += mobility
MOBILITY = location
This snippet requires the following capabilities:
symbian: {
TARGET.CAPABILITY = NetworkServices \
Location
}
Header
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
// QtMobility name space
QTM_USE_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
/**
* Paints the display.
*/
void paintEvent(QPaintEvent* paintEvent);
public slots:
/**
* Called when the network request finishes.
*/
void handleNetworkData(QNetworkReply* reply);
private:
/**
* Fetches the map image.
*/
void fetchMap(const QSize& size, qreal latitude, qreal longitude);
private:
QNetworkAccessManager networkAccessManager;
QPixmap mapPixmap;
}
Source
#include <QtGui/QImage>
#include <QtGui/QPainter>
#include <QtGui/QPixmap>
#include <QtNetwork/QNetworkRequest>
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
{
// UI-related code omitted for brevity
// When the network request finishes, call the function which handles
// the received data
QObject::connect(&networkAccessManager,
SIGNAL(finished(QNetworkReply*)),
this,
SLOT(handleNetworkData(QNetworkReply*)));
}
void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)
{
// Location fetching omitted here for brevity. Refer to the See also
// section for more information.
qreal latitude = geoCoordinate.latitude();
qreal longitude = geoCoordinate.longitude();
// Fetch the map using the display size and the coordinates
fetchMap(size(), latitude, longitude);
}
void MainWindow::fetchMap(const QSize& size, qreal latitude, qreal longitude)
{
const QString GOOGLE_MAPS_URL_TEMPLATE =
"http://maps.google.com/maps/api/staticmap?center=%1,%2&zoom=12&size=%3x%4&maptype=mobile&markers=color:red|label:Y|%1,%2&sensor=false";
QUrl url = QUrl(GOOGLE_MAPS_URL_TEMPLATE.arg(
QString::number(latitude), QString::number(longitude),
QString::number(size.width()), QString::number(size.height())));
QNetworkRequest request;
request.setUrl(url);
networkAccessManager.get(request);
}
void MainWindow::handleNetworkData(QNetworkReply* reply)
{
QImage image;
if (reply->error() == QNetworkReply::NoError) {
image.load(reply, 0);
if (!image.isNull()) {
// Convert QImage into QPixmap and store it into a member
// variable
mapPixmap = QPixmap::fromImage(image);
}
}
reply->deleteLater();
// Update the display
update();
}
void MainWindow::paintEvent(QPaintEvent* paintEvent)
{
// Draw the map tile onto the display
QPainter painter(this);
painter.drawPixmap(0, 0, mapPixmap);
}
Postconditions
A map tile is fetched from Google Maps, based on the current location, and painted on the display.


(no comments yet)