(Difference between revisions)

Showing a map in Qt using Nokia Maps

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix ArticleMetaData and links)
hamishwillee (Talk | contribs)
m (Hamishwillee - Remove Draft category as this looks essentially "done")
Line 1: Line 1:
[[Category:Draft]][[Category:Qt]][[Category:Qt WebKit]][[Category:Nokia Maps]][[Category:Symbian]][[Category:Code Snippet]]
[[Category:Qt]][[Category:Qt WebKit]][[Category:Nokia Maps]][[Category:Symbian]][[Category:Code Snippet]]
{{Abstract|This code snippet demonstrates how to fetch a map tile in Qt using the Location module of Qt Mobility and Nokia Maps.}}
{{Abstract|This code snippet demonstrates how to fetch a map tile in Qt using the Location module of Qt Mobility and Nokia Maps.}}

Revision as of 07:27, 20 September 2012

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 (20 Sep 2012)



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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- Add the JavaScript file resource into the document -->
<script src="http://api.maps.ovi.com/jsl.js" 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 (window.location.search.length > 1) {
var iCouple;
var aCouples = window.location.search.substr(1).split("&");
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 ovi.mapsapi.map.Display(
'zoomLevel': oGetVars["zoom"],
'center': centerCoords
// Create a marker and add it to the map
var marker = new ovi.mapsapi.map.StandardMarker(
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

106 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.