Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Showing a map in Qt using Nokia Maps

From Wiki
Jump to: navigation, search

This code snippet demonstrates how to fetch a map tile in Qt using the Location module of Qt Mobility and Nokia Maps.

Article Metadata
Tested with
Devices(s): Nokia C7-00
Platform(s): Symbian^3
Platform Security
Capabilities: Location, NetworkServices
Created: tapla (04 Jan 2012)
Last edited: hamishwillee (24 Apr 2013)



This code snippet demonstrates how to fetch a map tile in Qt using the Location module of Qt Mobility and Nokia Maps. It is assumed here that you have set up Qt Mobility in your development environment and on your device. For more information, see Setting up Qt Mobility.

Qt project file

Add the WebKit and the Location modules into the project:

QT += webkit
CONFIG += mobility
MOBILITY = location

This snippet requires the following capabilities:

symbian: {

Source: main.cpp

#include "mainwindow.h"
#include <QtGui/QApplication>
int main(int argc, char* argv[])
QApplication app(argc, argv);
MainWindow mainWindow;
return app.exec();

Header: mainwindow.h

#include <QtCore/QPointer>
#include <QtCore/QUrl>
#include <QtGui/QMainWindow>
#include <QtWebKit/QWebView>
// Location API
#include <QGeoPositionInfo>
#include <QGeoPositionInfoSource>
// QtMobility namespace
class MainWindow : public QMainWindow
* Constructor.

MainWindow(QWidget* parent = 0);
* Destructor.

public slots:
* Called when the current position is updated.

void positionUpdated(QGeoPositionInfo geoPositionInfo);
* Obtains the location data source and starts listening for position
* changes.

void startGPS();
* Creates the URL with which the map tile can be fetched.

QUrl createMapURL(const QSize& size, qreal latitude,
qreal longitude);
QPointer<QGeoPositionInfoSource> locationDataSource;
QWebView* webView;

Source: mainwindow.cpp

#include "mainwindow.h"
#include <QGeoCoordinate>
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
setWindowTitle("Nokia Maps Example");
webView = new QWebView(this);
// Start the GPS
void MainWindow::startGPS()
// Location fetching omitted here for brevity. Refer to the See also
// section for more information.
// Whenever the current position is updated, the positionUpdated
// function is called.
void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)
if (geoPositionInfo.isValid())
// Stop regular position updates, because a valid position has been
// obtained
// Get the current location as latitude and longitude
QGeoCoordinate location = geoPositionInfo.coordinate();
qreal latitude = location.latitude();
qreal longitude = location.longitude();
// Fetch the map using the display size and the coordinates
QUrl url = createMapURL(size(), latitude, longitude);
webView->stop(); // Stop the ongoing request
webView->load(url); // Load the map
QUrl MainWindow::createMapURL(const QSize& size, qreal latitude,
qreal longitude)
const QString MAP_URL_TEMPLATE =
QUrl url = QUrl(MAP_URL_TEMPLATE.arg(
QString::number(latitude), QString::number(longitude),
QString::number(size.width()), QString::number(size.height())));
return url;

Source: map.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- Add the JavaScript file resource into the document -->
<script src="" type="text/javascript" charset="utf-8"></script>
<div id="mapContainer">
<script type="text/javascript">
// Retrieve the GET parameters from the query string
var oGetVars = {};
function buildValue(sValue) {
if (/^\s*$/.test(sValue)) {
if (/^(true|false)$/i.test(sValue)) {
return(sValue.toLowerCase() === "true");
if (isFinite(sValue)) {
if (isFinite(Date.parse(sValue))) {
return(new Date(sValue));
if ( > 1) {
var iCouple;
var aCouples ="&");
for (var iCouplId = 0; iCouplId < aCouples.length; iCouplId++) {
iCouple = aCouples[iCouplId].split("=");
oGetVars[unescape(iCouple[0])] = iCouple.length > 1 ?
buildValue(unescape(iCouple[1])) :
} else {
throw "No GET parameters.";
// Set the size of the map element
var mapContainerElement = document.getElementById("mapContainer");
var styleAttr = "width:" + oGetVars["sizex"] + "px; " +
"height:" + oGetVars["sizey"] + "px;"
mapContainerElement.setAttribute("style", styleAttr);
// Create the map and display it in mapContainerElement
var centerCoords = [oGetVars["centerx"], oGetVars["centery"]];
var map = new
'zoomLevel': oGetVars["zoom"],
'center': centerCoords
// Create a marker and add it to the map
var marker = new
text: "Y"

Add the map.html file to the resource file:

<qresource prefix="/">


This code snippet demonstrated how to fetch a map from Nokia Maps, based on the current location, and show it on the display.

See also

This page was last modified on 24 April 2013, at 03:22.
58 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.