Meego Wiki
Views

QML/Get GPS data

From MeeGo wiki
(Difference between revisions)
Jump to: navigation, search
(How to get GPS data in QML?)
 
Line 1: Line 1:
-
= How to get GPS data in QML? =
+
= How to get GPS data in QML =
Code snippet that draws ui and shows GPS data and accuracy, also with GPS on/off switch if one wants to conserve power.
Code snippet that draws ui and shows GPS data and accuracy, also with GPS on/off switch if one wants to conserve power.
Line 5: Line 5:
* Made by: Ossipena
* Made by: Ossipena
* Modified by: Venemo
* Modified by: Venemo
 +
* Modified by: texrat
== main.qml ==
== main.qml ==
<pre>
<pre>
 +
// original code by Ossipena.
 +
// Modified by Venemo and Texrat
 +
import QtQuick 1.1
import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.meego 1.0
Line 14: Line 18:
     id: appWindow
     id: appWindow
     initialPage: mainPage
     initialPage: mainPage
 +
    showStatusBar: true
     MainPage {
     MainPage {
Line 23: Line 28:
         parent: appWindow.pageStack.currentPage
         parent: appWindow.pageStack.currentPage
-
         Button {
+
         Item {
-
             text: "View trackings"
+
             id: switchRow
-
            height: parent.height
+
            width: 300
 +
            height: 32
 +
            anchors.verticalCenter: parent.verticalCenter
 +
 
 +
            // Switch for power saving: toggle gps on/off
 +
            Switch {
 +
                id: gpsSwitch
 +
                anchors.verticalCenter: parent.verticalCenter
 +
                checked: false
 +
                anchors.right: parent.right
 +
                anchors.rightMargin: 50
 +
            }
 +
            Text {
 +
                id:switchText
 +
                font.pointSize: 24
 +
                text: gpsSwitch.checked ? "GPS ON" : "GPS OFF"
 +
                horizontalAlignment: Text.AlignRight
 +
                anchors.verticalCenter: parent.verticalCenter
 +
                anchors.right: gpsSwitch.left
 +
                anchors.rightMargin: 25
 +
                font.family: "Nokia Pure"
 +
            }
 +
 
         }
         }
 +
// menu not yet implemented
         ToolIcon {
         ToolIcon {
             platformIconId: "toolbar-view-menu";
             platformIconId: "toolbar-view-menu";
Line 39: Line 67:
import QtQuick 1.1
import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.meego 1.0
 +
import com.nokia.extras 1.1
import Qt 4.7
import Qt 4.7
-
import QtMobility.location 1.1
+
import QtMobility.location 1.2
 +
//import QtMultimediaKit 1.1  //for future audio handling
Page {
Page {
     id: mainPage
     id: mainPage
     tools: commonTools
     tools: commonTools
 +
     function printableMethod(method) {
     function printableMethod(method) {
Line 56: Line 87:
             return "All/multiple"
             return "All/multiple"
         return "source error";
         return "source error";
 +
    }
 +
 +
    // the following function handles switching between
 +
    // kilometers per hour and miles per hour for speed
 +
    function speedConvert(speedState) {
 +
        if (speedState == buttonMetric)
 +
            return 1.00;
 +
        else
 +
            return 0.62;
 +
    }
 +
    // the following function handles switching between
 +
    // meters and feet for altitude
 +
    function heightConvert(heightState) {
 +
        if (heightState == buttonMetric)
 +
            return 1.00;
 +
        else
 +
            return 3.28;
     }
     }
Line 63: Line 111:
         updateInterval: 1000
         updateInterval: 1000
         //active: false
         //active: false
-
         active: gpsswitch.checked
+
         active: gpsSwitch.checked
     }
     }
-
     Grid {
+
     Item {
-
         id: positiondata
+
         id: rowItem
 +
        width: 360
 +
        anchors.horizontalCenter: parent.horizontalCenter
         anchors.top: parent.top
         anchors.top: parent.top
 +
        anchors.topMargin: 25
 +
        height: 32
 +
 +
        ButtonRow {
 +
            id: unitRow
 +
            x: -115
 +
            y: 0
 +
            width: 230
 +
            height: 32
 +
            anchors.horizontalCenterOffset: 0
 +
            anchors.horizontalCenter: parent.horizontalCenter
 +
            anchors.top: parent.top
 +
            anchors.topMargin: 0
 +
            z: 1
 +
            Button {
 +
                id: buttonImperial
 +
                text: "Imperial"
 +
            }
 +
            Button {
 +
                id: buttonMetric
 +
                text: "Metric"
 +
            }
 +
        }
 +
    }
 +
 +
    Grid {
 +
        id: positionData
 +
        x: 10
 +
        y: 50
 +
        width: 891
 +
        height: 268
 +
        anchors.top: rowItem.bottom
         anchors.topMargin: 50
         anchors.topMargin: 50
         anchors.left:  parent.left
         anchors.left:  parent.left
Line 76: Line 158:
         Text {
         Text {
-
             id:latitudelabel
+
             id: speedLabel
-
             font.pointSize: 32
+
            text: "Speed:"
 +
            font.pointSize: 24
 +
            font.family: "Nokia Pure"
 +
        }
 +
        // units are returned in meters per second by default
 +
        //here they are converted to either kph or mph
 +
        //using factor shown along with unitConvert function
 +
        Text{
 +
            id: speed
 +
            font.pointSize: 24
 +
            text: (3.6 * gpsSwitch.checked) * speedConvert(unitRow.checkedButton) * positionSource.position.speed.toFixed(2)
 +
            font.family: "Nokia Pure"
 +
        }
 +
        Text {
 +
            id: latitudeLabel
 +
             font.pointSize: 24
             text: "Latitude:"
             text: "Latitude:"
 +
            font.family: "Nokia Pure"
         }
         }
         Text {
         Text {
-
             id: latitudetext
+
             id: latitudeText
-
             font.pointSize: 32
+
             font.pointSize: 24
-
             text: positionSource.position.coordinate.latitude
+
             text: positionSource.position.coordinate.latitude.toFixed(5)
 +
            font.family: "Nokia Pure"
         }
         }
         Text {
         Text {
-
             id:longitudelabel
+
             id: longitudeLabel
-
             font.pointSize: 32
+
             font.pointSize: 24
-
             text: "Longitude: "
+
             text: "Longitude:"
 +
            font.family: "Nokia Pure"
         }
         }
         Text {
         Text {
-
             id: longitudetext
+
             id: longitudeText
-
             font.pointSize: 32
+
             font.pointSize: 24
-
             text: positionSource.position.coordinate.longitude
+
             text: positionSource.position.coordinate.longitude.toFixed(5)
 +
            font.family: "Nokia Pure"
         }
         }
         Text{
         Text{
-
             id:accuracylabel1
+
             id: accuracyLabel1
-
             font.pointSize: 16
+
             font.pointSize: 18
             text: "Horizontal accuracy:"
             text: "Horizontal accuracy:"
 +
            font.family: "Nokia Pure"
         }
         }
         Text{
         Text{
-
             id: horiz_accuracy
+
             id: horiz_accuracyText
-
             font.pointSize: 16
+
             font.pointSize: 18
-
             text: positionSource.position.horizontalAccuracy
+
             text: positionSource.position.horizontalAccuracy.toFixed(2)
 +
            font.family: "Nokia Pure"
         }
         }
         Text {
         Text {
-
             id:heightlabel
+
             id:heightLabel
-
             font.pointSize: 32
+
             font.pointSize: 24
-
             text: "Height: "
+
             text: "Height:"
 +
            font.family: "Nokia Pure"
         }
         }
         Text {
         Text {
-
             id: heighttext
+
             id: heightText
-
             font.pointSize: 32
+
             font.pointSize: 24
-
             text: positionSource.position.coordinate.altitude
+
             text: heightConvert(unitRow.checkedButton) * positionSource.position.coordinate.altitude.toFixed(5)
 +
            font.family: "Nokia Pure"
         }
         }
         Text{
         Text{
-
             id:accuracylabel2
+
             id: accuracyLabel2
-
             font.pointSize: 16
+
             font.pointSize: 18
             text: "Altitude accuracy:"
             text: "Altitude accuracy:"
 +
            font.family: "Nokia Pure"
         }
         }
         Text{
         Text{
-
             id: vert_accuracy
+
             id: vert_accuracyText
-
             font.pointSize: 16
+
             font.pointSize: 18
-
             text: positionSource.position.verticalAccuracy
+
             text: positionSource.position.verticalAccuracy.toFixed(2)
-
        }
+
             font.family: "Nokia Pure"
-
    }
+
-
    Item {
+
-
        id: switchrow
+
-
        width: 300
+
-
        anchors.top:  positiondata.bottom
+
-
        anchors.left:  positiondata.left
+
-
        anchors.topMargin: 50
+
-
 
+
-
        // Switch for power saving: toggle gps on/off
+
-
        Text {
+
-
            id:switchtext
+
-
            anchors.left: parent.left
+
-
             font.pointSize: 32
+
-
            text: gpsswitch.checked ? "GPS ON" : "GPS OFF"
+
-
        }
+
-
        Switch {
+
-
            id: gpsswitch
+
-
            checked: false
+
-
            anchors.left: switchtext.right
+
-
            anchors.leftMargin: 50
+
         }
         }
     }
     }
}
}
</pre>
</pre>

Latest revision as of 03:38, 30 November 2011

How to get GPS data in QML

Code snippet that draws ui and shows GPS data and accuracy, also with GPS on/off switch if one wants to conserve power.

  • Made by: Ossipena
  • Modified by: Venemo
  • Modified by: texrat

main.qml

// original code by Ossipena.
// Modified by Venemo and Texrat

import QtQuick 1.1
import com.nokia.meego 1.0

PageStackWindow {
    id: appWindow
    initialPage: mainPage
    showStatusBar: true

    MainPage {
        id: mainPage
    }
    ToolBarLayout {
        id: commonTools
        visible: true
        parent: appWindow.pageStack.currentPage

        Item {
            id: switchRow
            width: 300
            height: 32
            anchors.verticalCenter: parent.verticalCenter

            // Switch for power saving: toggle gps on/off
            Switch {
                id: gpsSwitch
                anchors.verticalCenter: parent.verticalCenter
                checked: false
                anchors.right: parent.right
                anchors.rightMargin: 50
            }
            Text {
                id:switchText
                font.pointSize: 24
                text: gpsSwitch.checked ? "GPS ON" : "GPS OFF"
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: gpsSwitch.left
                anchors.rightMargin: 25
                font.family: "Nokia Pure"
            }

        }
// menu not yet implemented
        ToolIcon {
            platformIconId: "toolbar-view-menu";
        }
    }
}

MainPage.qml

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import Qt 4.7
import QtMobility.location 1.2
//import QtMultimediaKit 1.1  //for future audio handling

Page {
    id: mainPage
    tools: commonTools


    function printableMethod(method) {
        if (method == PositionSource.SatellitePositioningMethod)
            return "Satellite";
        else if (method == PositionSource.NoPositioningMethod)
            return "Not available";
        else if (method == PositionSouce.NonSatellitePositioningMethod)
            return "Non-satellite";
        else if (method == PositionSource.AllPositioningMethods)
            return "All/multiple"
        return "source error";
    }

    // the following function handles switching between
    // kilometers per hour and miles per hour for speed
    function speedConvert(speedState) {
        if (speedState == buttonMetric)
            return 1.00;
        else
            return 0.62;
    }
    // the following function handles switching between
    // meters and feet for altitude
    function heightConvert(heightState) {
        if (heightState == buttonMetric)
            return 1.00;
        else
            return 3.28;
    }

    //init GPS etc
    PositionSource {
        id: positionSource
        updateInterval: 1000
        //active: false
        active: gpsSwitch.checked
    }

    Item {
        id: rowItem
        width: 360
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: parent.top
        anchors.topMargin: 25
        height: 32

        ButtonRow {
            id: unitRow
            x: -115
            y: 0
            width: 230
            height: 32
            anchors.horizontalCenterOffset: 0
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.topMargin: 0
            z: 1
            Button {
                id: buttonImperial
                text: "Imperial"
            }
            Button {
                id: buttonMetric
                text: "Metric"
            }
        }
    }

    Grid {
        id: positionData
        x: 10
        y: 50
        width: 891
        height: 268
        anchors.top: rowItem.bottom
        anchors.topMargin: 50
        anchors.left:  parent.left
        anchors.leftMargin: 10
        columns: 2
        spacing: 5

        Text {
            id: speedLabel
            text: "Speed:"
            font.pointSize: 24
            font.family: "Nokia Pure"
        }
        // units are returned in meters per second by default
        //here they are converted to either kph or mph
        //using factor shown along with unitConvert function
        Text{
            id: speed
            font.pointSize: 24
            text: (3.6 * gpsSwitch.checked) * speedConvert(unitRow.checkedButton) * positionSource.position.speed.toFixed(2)
            font.family: "Nokia Pure"
        }
        Text {
            id: latitudeLabel
            font.pointSize: 24
            text: "Latitude:"
            font.family: "Nokia Pure"
        }
        Text {
            id: latitudeText
            font.pointSize: 24
            text: positionSource.position.coordinate.latitude.toFixed(5)
            font.family: "Nokia Pure"
        }
        Text {
            id: longitudeLabel
            font.pointSize: 24
            text: "Longitude:"
            font.family: "Nokia Pure"
        }
        Text {
            id: longitudeText
            font.pointSize: 24
            text: positionSource.position.coordinate.longitude.toFixed(5)
            font.family: "Nokia Pure"
        }
        Text{
            id: accuracyLabel1
            font.pointSize: 18
            text: "Horizontal accuracy:"
            font.family: "Nokia Pure"
        }
        Text{
            id: horiz_accuracyText
            font.pointSize: 18
            text: positionSource.position.horizontalAccuracy.toFixed(2)
            font.family: "Nokia Pure"
        }
        Text {
            id:heightLabel
            font.pointSize: 24
            text: "Height:"
            font.family: "Nokia Pure"
        }
        Text {
            id: heightText
            font.pointSize: 24
            text: heightConvert(unitRow.checkedButton) * positionSource.position.coordinate.altitude.toFixed(5)
            font.family: "Nokia Pure"
        }
        Text{
            id: accuracyLabel2
            font.pointSize: 18
            text: "Altitude accuracy:"
            font.family: "Nokia Pure"
        }
        Text{
            id: vert_accuracyText
            font.pointSize: 18
            text: positionSource.position.verticalAccuracy.toFixed(2)
            font.family: "Nokia Pure"
        }
    }
}
Personal tools