GPS tracker on Qt, map and track

    I decided to practice programming for Android on Qt. I chose a GPS tracker as a topic.
    The set of functions of this tracker:


    • take measurements from the GPS receiver;
    • export track to GPX ( GPS eXchange Format );
    • display the track on the map;
    • display travel time, path length, average speed.

    Under the cut will be an example of working with a map in QtQuick.



    Plugin, PluginParameter and Map


    The basic types are in the Qt Location module. Qt Location as a backend can use:



    Work with a specific map provider is placed in plugins ( Plugin ), which are configured through parameters ( PluginParameter ).


    Minimal example:


    Plugin {
      id: plugin
      preferred: ["here", "osm"]
      required: Plugin.AnyMappingFeatures | Plugin.AnyGeocodingFeatures
    }
    Map {
        plugin: plugin
        width: ...
        height:...
    }

    At first, I just used the Open Street Map:


    Plugin {
         id: plugin
         name: "osm"
    }
    

    The default provider of OSM maps is MapQuest , which has recently introduced a developer key. Then the question arose of switching to something else.


    Open street map plugin


    The documentation lists support for:



    In order to use the last item, you must fulfill two conditions:


    Set the osm.mapping.host parameter :


    Plugin {
         id: mapPlugin
         name: "osm"
         PluginParameter {
             name: "osm.mapping.host";
             value: "http://a.tile.openstreetmap.org/"
         }
    }

    Map the map to use MapType.CustomMap map type .
    For lovers of magic:


    Map {
        id: map
        plugin: mapPlugin
        activeMapType: map.supportedMapTypes[7]
    }

    In order not to rely on the position of an element in the supportedMapTypes list , you can do this:


    Map {
        id: map
        plugin: mapPlugin
        zoomLevel: 16
        width: item.width
        height:item.height
        property MapPolyline track
    }
    Timer {
        interval: 100; running: true; repeat: false
        onTriggered: {
            for(var i = 0;
                i < map.supportedMapTypes.length;
                ++i){
                if(map.supportedMapTypes[i].style
                        === MapType.CustomMap){
                    map.activeMapType = map.supportedMapTypes[i];
                }
            }
        }
    }

    Track


    To draw a track, I took the MapPolyline element , while creating it dynamically, to clear the map:


    function start() {
        mapItem.clearMapItems();
        mapItem.track = Qt.createQmlObject('import QtLocation 5.6; MapPolyline {}', item);
        mapItem.track.line.width = 6;
        mapItem.track.line.color = 'red';
        mapItem.addMapItem(mapItem.track);
    }
    function appendCoordinate(position){
        mapItem.center = position;
        mapItem.track.addCoordinate(position)
    }
    Map {
        id: mapItem
        plugin: mapPlugin
        zoomLevel: 16
        width: item.width
        height:item.height
        property MapPolyline track
    }

    At start, I clear the map, create a track and place it on the map. When adding a new coordinate, the center of the map moves to the specified position and the track lengthens.


    Result:



    Code Available on GitHub


    Also popular now: