Skip to content

Positioning

In this guide you will learn how to use different techniques to add positioning to your application.

SimpleMap with positioning

First, get an API key token, see the Getting Started guide.

Qt should be installed to continue.

The Maps SDK for Qt should be installed, see the Setup Maps SDK for Qt guide.

Overview

So, let’s add positioning support to the SimpleMap example.

GeneralMagic QML plugin supports a few ways to feed the navigation engine with positions:

1) Use Qt’s PositionSource. PositionSource has plenty of plugins which should support most of the situations out there.

2) Feed the engine with external positions. This should be by far your last choice, and it should be used only if PositionSource can not be used for this job.

Using Qt’s PositionSource

Qt’s PositionSource is by far the easiest way to feed GeneralMagic with positions. As mentioned above, we do recommend you to use this option. There is no need to duplicate the excellent PositionSource documentation; we suggest you read it

here

Following are some examples to walk you through the major plugins:

For Android, iOS there is no need for any special configurations to PositionSource

1// QML code
2import QtPositioning 5.12
3// ...
4    PositionSource {
5        id: qtPositionSource
6        active: true
7        Component.onCompleted: ServicesManager.dataSource.setPositionSource(qtPositionSource)
8    }
9// ...

Yes, that’s all you need to do to feed GeneralMagic with positions!

For (embedded) linux this is a little more tricky. First and foremost it depends on the device itself. If your device is supported by one of the existing plugins (e.g. geoclue, gypsy) then you need first to setup the daemons, then just specify which plugin you’re going to use.

Let’s say your embedded system has setup gypsy daemon; in this case your QML code will look like this:

 1// QML code
 2import QtPositioning 5.12
 3// ...
 4    PositionSource {
 5        id: qtPositionSource
 6        name: "gypsy"
 7        active: true
 8        Component.onCompleted: ServicesManager.dataSource.setPositionSource(qtPositionSource)
 9    }
10// ...

Most of the GPS devices support serial NMEA communication; if using gypsy or geoclue is too computation intensive for your device, you can use

serialnmea

plugin instead which is pretty lightweight. In this case your QML code will be slightly longer as we need to also specify the port:

 1// QML code
 2import QtPositioning 5.12
 3// ...
 4    PositionSource {
 5        id: qtPositionSource
 6        name: "serialnmea"
 7        active: true
 8
 9        // assuming that your GPS receiver is registered as /dev/ttyUSB0, we need to tell serialnmea plugin about it:
10        PluginParameter { name: "serialnmea.serial_port"; value: "ttyUSB0" }
11        // alternatively you can set QT_NMEA_SERIAL_PORT env variable to ttyUSB0 and you can drop the PluginParameter line
12
13        Component.onCompleted: ServicesManager.dataSource.setPositionSource(qtPositionSource)
14    }
15// ...

Custom crafted positions

If there is no way to use PositionSource then you can still feed the GeneralMagic QML plugin with positions. Here is a mock QML example which should give you an idea how to push the positions to the engine:

 1// QML code
 2// ...
 3// Custom crafted positions
 4function feedEngine(externalPos)
 5{
 6    let pos = ServicesManager.createPosition();
 7    // mandatory fields
 8    pos.timeStamp = externalPos.time_stamp; // do not use new Date() !
 9    pos.latitude = externalPos.latitude;
10    pos.longitude = externalPos.longitude;
11    pos.horizontalAccuracy = externalPos.horizontal_accuracy;
12    pos.speed = externalPos.speed;
13    pos.course = externalPos.course;
14    pos.courseAccuracy = externalPos.course_accuracy;
15
16    // optional fields. All these fields are filled with default values,
17    // so if you don't have valid values, do no set them
18    if (externalPos.has_altitude)
19        pos.altitude = externalPos.altitude;
20    if (externalPos.has_vertical_accuracy)
21        pos.verticalAccuracy = externalPos.vertical_accuracy;
22    if (externalPos.has_speed_accuracy)
23        pos.speedAccuracy = externalPos.speed_accuracy;
24
25    // finally push the position to the engine
26    ServicesManager.dataSource.pushPosition(pos);
27}
28
29// Let's say you have custom position source named MyCustomPositionSource
30// which will notify us via PositionUpdated signal when it has a new position
31MyCustomPositionSource {
32    onPositionUpdated: feedEngine(position)
33}

Be aware that failing to set all the mandatory fields with sane data, will break the GeneralMagic behavior. E.g. setting timeStamp to something like new Date() most of the time will not work properly as it doesn’t come from the GPS device and it will be slightly shifted. Optional fields are also very important and they should be omitted only if that data is not available at all.

QML Examples

Maps SDK for Qt Examples can be downloaded or cloned with Git