(→How to get GPS data in QML?) |
|||
| (3 intermediate revisions not shown) | |||
| Line 1: | Line 1: | ||
| + | = 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. | ||
| + | * Made by: Ossipena | ||
| + | * Modified by: Venemo | ||
| + | * Modified by: texrat | ||
| + | |||
| + | == main.qml == | ||
<pre> | <pre> | ||
| - | // | + | // original code by Ossipena. |
| - | + | // Modified by Venemo and Texrat | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
import QtQuick 1.1 | import QtQuick 1.1 | ||
| Line 14: | Line 17: | ||
PageStackWindow { | PageStackWindow { | ||
id: appWindow | id: appWindow | ||
| + | initialPage: mainPage | ||
| + | showStatusBar: true | ||
| - | MainPage {id: mainPage} | + | MainPage { |
| - | + | id: mainPage | |
| - | + | } | |
| - | + | ||
ToolBarLayout { | ToolBarLayout { | ||
id: commonTools | id: commonTools | ||
visible: true | 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 | |
| - | text: " | + | 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"; | + | ToolIcon { |
| - | + | platformIconId: "toolbar-view-menu"; | |
| - | + | ||
| - | + | ||
} | } | ||
| - | |||
} | } | ||
| - | |||
| - | |||
} | } | ||
| + | </pre> | ||
| + | == MainPage.qml == | ||
| - | + | <pre> | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
import QtQuick 1.1 | import QtQuick 1.1 | ||
| - | import com.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) { | ||
if (method == PositionSource.SatellitePositioningMethod) | if (method == PositionSource.SatellitePositioningMethod) | ||
| Line 76: | Line 88: | ||
return "source error"; | return "source error"; | ||
} | } | ||
| - | + | ||
| - | id: | + | // 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.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 84: | Line 156: | ||
columns: 2 | columns: 2 | ||
spacing: 5 | 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 { | Text { | ||
| - | id: | + | id: latitudeLabel |
| - | font.pointSize: | + | font.pointSize: 24 |
text: "Latitude:" | text: "Latitude:" | ||
| + | font.family: "Nokia Pure" | ||
} | } | ||
Text { | Text { | ||
| - | id: | + | id: latitudeText |
| - | font.pointSize: | + | font.pointSize: 24 |
| - | text: positionSource.position.coordinate.latitude | + | text: positionSource.position.coordinate.latitude.toFixed(5) |
| + | font.family: "Nokia Pure" | ||
} | } | ||
Text { | Text { | ||
| - | id: | + | id: longitudeLabel |
| - | font.pointSize: | + | font.pointSize: 24 |
| - | text: "Longitude: " | + | text: "Longitude:" |
| + | font.family: "Nokia Pure" | ||
} | } | ||
Text { | Text { | ||
| - | id: | + | id: longitudeText |
| - | font.pointSize: | + | font.pointSize: 24 |
| - | text: positionSource.position.coordinate.longitude | + | text: positionSource.position.coordinate.longitude.toFixed(5) |
| + | font.family: "Nokia Pure" | ||
} | } | ||
Text{ | Text{ | ||
| - | id: | + | id: accuracyLabel1 |
| - | font.pointSize: | + | font.pointSize: 18 |
text: "Horizontal accuracy:" | text: "Horizontal accuracy:" | ||
| + | font.family: "Nokia Pure" | ||
} | } | ||
Text{ | Text{ | ||
| - | id: | + | id: horiz_accuracyText |
| - | font.pointSize: | + | font.pointSize: 18 |
| - | text: positionSource.position.horizontalAccuracy | + | text: positionSource.position.horizontalAccuracy.toFixed(2) |
| + | font.family: "Nokia Pure" | ||
} | } | ||
Text { | Text { | ||
| - | id: | + | id:heightLabel |
| - | font.pointSize: | + | font.pointSize: 24 |
| - | text: "Height: " | + | text: "Height:" |
| + | font.family: "Nokia Pure" | ||
} | } | ||
Text { | Text { | ||
| - | id: | + | id: heightText |
| - | font.pointSize: | + | 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: | + | id: accuracyLabel2 |
| - | font.pointSize: | + | font.pointSize: 18 |
text: "Altitude accuracy:" | text: "Altitude accuracy:" | ||
| + | font.family: "Nokia Pure" | ||
} | } | ||
Text{ | Text{ | ||
| - | id: | + | id: vert_accuracyText |
| - | font.pointSize: | + | font.pointSize: 18 |
| - | text: positionSource.position.verticalAccuracy | + | text: positionSource.position.verticalAccuracy.toFixed(2) |
| + | font.family: "Nokia Pure" | ||
} | } | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
} | } | ||
} | } | ||
| - | |||
| - | |||
| - | |||
</pre> | </pre> | ||
Code snippet that draws ui and shows GPS data and accuracy, also with GPS on/off switch if one wants to conserve power.
// 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";
}
}
}
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"
}
}
}