Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Location Services

Introduction

The Qt Extended Whereabouts library provides developers of Location-Based Services with the essential components for distributing, receiving and processing location information. The library can be used to easily query and retrieve location information, and location updates can be received from either built-in or custom-added location data sources. Custom location data sources are added through Qt Extended's plugin mechanisms.

The main classes in the Whereabouts library are QWhereabouts, QWhereaboutsUpdate, QWhereaboutsPlugin and QWhereaboutsFactory:

In addition, QNmeaWhereabouts provides functionality for reading location data from NMEA data sources. Developers can use it to read NMEA data sources without the need to write NMEA parsing code in their plugin implementations.

A simple example

The Whereabouts Simple Demo demonstrates how to create a QWhereabouts instance and use it to retrieve location updates:

    class SimpleLocationDemo : public QObject
    {
        Q_OBJECT
    public:
        SimpleLocationDemo(QObject *parent = 0)
            : QObject(parent)
        {
            QWhereabouts *whereabouts = QWhereaboutsFactory::create();
            connect(whereabouts, SIGNAL(updated(QWhereaboutsUpdate)),
                    SLOT(updated(QWhereaboutsUpdate)));

            whereabouts->startUpdates();
        }

    private slots:
        void updated(const QWhereaboutsUpdate &update)
        {
            // respond to update here
        }
    };

The application simply calls QWhereabouts::startUpdates() to begin to receive regular location updates through the QWhereabouts::updated() signal.

Where does the location data come from? That depends on the QWhereabouts instance returned from QWhereaboutsFactory::create(). In this example, create() is called without any arguments, so the returned instance receives location data from the default data source. See below for details on using the default and custom data sources.

Reading location data from QWhereaboutsUpdate

QWhereaboutsUpdate contains the various types of data that can be retrieved in a location update. At a minimum, an update includes a global coordinate position specified by a QWhereaboutsCoordinate object, and a timestamp specified by QWhereaboutsUpdate::updateDateTime() that indicates when the update was received. The update may also include other information such as altitude, ground speed and bearing. QWhereaboutsUpdate::dataValidityFlags() indicates the types of data contained in a particular update.

Coordinates are expected to be in the WGS84 datum, which is the most commonly used datum for GPS devices and is a standard datum for worldwide use. Also note that location data generally uses metric units; altitude is measured in metres and speed is measured in kilometres/hour.

Specifying and adding location data sources

The Whereabouts API uses Qt Extended's plugin mechanisms to allow location data to be retrieved from arbitrary sources, such as custom GPS hardware devices, AGPS sources, or previously logged location data. Developers can write plugins that retrieve data from their desired data source and then distribute this data through the Whereabouts API classes.

The desired location data source is specified by the argument to QWhereaboutsFactory::create(). If no argument is provided, the default data source is used. Otherwise, the argument can be set to the class name of the plugin that provides the location data.

As a convenience, the Whereabouts library has a built-in plugin, "gpsd", that retrieves data from GPSd daemons. GPSd is an open-source service daemon that connects to a specified GPS device and distributes the GPS data over a TCP port. The gpsd plugin connects to this daemon and then provides access to the data through the Whereabouts API. Naturally, the system integrator must ensure a GPSd daemon has been started on the defined GPSd port (2947) or else this plugin will not be able to receive any location data.

The default location data source

A default location data source can be specified through the "Plugins/Default" value in the $QPEDIR/etc/Settings/Trolltech/Whereabouts.conf settings file. For most Qt Extended device configurations, the default plugin is set to the built-in plugin, "gpsd". However, if your device has a supported Qt Extended device configuration and also has built-in GPS hardware, this default value may already be set to a plugin that can read from that hardware.

To use the default plugin, call QWhereaboutsFactory::create() without any arguments.

Adding custom location data sources

Whereabouts plugins can easily be added to retrieve location data from custom sources.

To add a Whereabouts plugin:

  1. Create a subclass of QWhereabouts that connects to and distributes data from your custom data source. (Or, if the data source provides location data in the form of NMEA sentences, you can use QNmeaWhereabouts instead of writing your own subclass.)
  2. Create a subclass of QWhereaboutsPlugin that is declared with the QTOPIA_PLUGIN_EXPORT macro, and when implementing QWhereaboutsPlugin::create(), return an instance of your QWhereaboutsSubclass. Also, export the plugin using the QTOPIA_EXPORT_PLUGIN macro.

Once the plugin is installed, you can use it by calling QWhereaboutsFactory::create() with the plugin class name.

An example

The Whereabouts Sample Plugin demonstrates the integration of a custom Whereabouts plugin. The examples/sampleplugin/locationplugin directory contains the plugin, and examples/sampleplugin/mylocationapp contains an application that uses the plugin to retrieve location updates.

First, we create a QWhereabouts subclass called LocationProvider:

    class LocationProvider : public QWhereabouts
    {
        Q_OBJECT
    public:
        LocationProvider(QObject *parent = 0);

        void requestUpdate();
        void startUpdates();
        void stopUpdates();

    private:
        QTimer *m_timer;
    };

This LocationProvider class simply produces updates with the current date/time and latitude-longitude coordinates of (0, 0). It uses QTimer to implement timed updates for QWhereabouts::startUpdates() and QWhereabouts::stopUpdates(). Here is the implementation:

    LocationProvider::LocationProvider(QObject *parent)
        : QWhereabouts(QWhereabouts::TerminalBasedUpdate, parent),
          m_timer(new QTimer(this))
    {
        connect(m_timer, SIGNAL(timeout()), SLOT(requestUpdate()));
    }

    void LocationProvider::requestUpdate()
    {
        QWhereaboutsUpdate update;
        update.setCoordinate(QWhereaboutsCoordinate(0.0, 0.0));
        update.setUpdateDateTime(QDateTime::currentDateTime());
        emitUpdated(update);
    }

    void LocationProvider::startUpdates()
    {
        if (updateInterval() > 0)
            m_timer->start(updateInterval());
        else
            m_timer->start(1000);
    }

    void LocationProvider::stopUpdates()
    {
        m_timer->stop();
    }

Then we create a Whereaboutsplugin called LocationPlugin that is able to create and return instances of LocationProvider. Here is the class definition:

    class QTOPIA_PLUGIN_EXPORT LocationPlugin : public QWhereaboutsPlugin
    {
        Q_OBJECT
    public:
        LocationPlugin(QObject *parent = 0);

        QWhereabouts *create(const QString &source);
    };

In the locationplugin.cpp file, we implement create() to return an instance of LocationProvider, and export the plugin using the QTOPIA_EXPORT_PLUGIN macro:

    LocationPlugin::LocationPlugin(QObject *parent)
        : QWhereaboutsPlugin(parent)
    {
    }

    QWhereabouts *LocationPlugin::create(const QString &source)
    {
        Q_UNUSED(source);
        return new LocationProvider;
    }

    QTOPIA_EXPORT_PLUGIN(LocationPlugin)

Then, once the plugin has been installed with qbuild image, you can use it by passing the class name to QWhereaboutsFactory::create(). (The class name argument is not case-sensitive.) So in the examples/sampleplugin/mylocationapp project, we create a MyLocationApp class that uses our custom plugin, like this:

    MyLocationApp::MyLocationApp(QWidget *parent, Qt::WFlags flags)
        : QMainWindow(parent, flags)
    {
        QWhereabouts *whereabouts = QWhereaboutsFactory::create("locationplugin");
        connect(whereabouts, SIGNAL(updated(QWhereaboutsUpdate)),
                SLOT(updated(QWhereaboutsUpdate)));

        whereabouts->startUpdates();
    }

    void MyLocationApp::updated(const QWhereaboutsUpdate &update)
    {
        // Respond to update here
    }

So MyLocationApp now receives updates through the LocationProvider we created.

To make this the default plugin to be returned if QWhereaboutsFactory::create() is called without any arguments, set the "Plugins/Default" value in $QPEDIR/etc/Settings/Trolltech/Whereabouts.conf to "locationplugin".

Other examples

The Whereabouts Mapping Demo uses the Whereabouts API together with Google Maps to either track the user's current location, or provide a simulation of a previous journey using a NMEA data log.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 80
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 6
Page suivante

Le Qt Developer Network au hasard

Logo

QML et les styles

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt qtextended4.4
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web