Meego Wiki
Views

QML/Get GPS data

From MeeGo wiki
Revision as of 03:38, 30 November 2011 by Texrat (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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