let speedText;
let unitText;
let isKmh = true;
let geolocation = null;
Page({
build() {
speedText = hmUI.createWidget(hmUI.widget.TEXT, {
x: 20,
y: 110,
w: 380,
h: 120,
color: 0xffffff,
text_size: 70,
align_h: hmUI.align.CENTER,
align_v: hmUI.align.CENTER_V,
text: "0.0"
});
unitText = hmUI.createWidget(hmUI.widget.TEXT, {
x: 20,
y: 230,
w: 380,
h: 50,
color: 0x00ff00,
text_size: 30,
align_h: hmUI.align.CENTER,
align_v: hmUI.align.CENTER_V,
text: "км/ч"
});
hmUI.createWidget(hmUI.widget.BUTTON, {
x: 0,
y: 0,
w: 480,
h: 480,
text: "",
press_color: 0x111111,
normal_color: 0x000000,
click_func: () => {
isKmh = !isKmh;
unitText.setProperty(hmUI.prop.TEXT, isKmh ? "км/ч" : "м/с");
}
});
this.initGPS();
},
initGPS() {
try {
geolocation = hmSetting.createGeolocation ? hmSetting.createGeolocation() : null;
if (geolocation) {
geolocation.start((data) => {
let rawSpeed = data.speed || 0;
if (rawSpeed < 0) rawSpeed = 0;
let displaySpeed = isKmh ? rawSpeed * 3.6 : rawSpeed;
speedText.setProperty(hmUI.prop.TEXT, displaySpeed.toFixed(1));
});
}
} catch (e) {}
},
onDestroy() {
if (geolocation) {
geolocation.stop();
}
}
});