-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBasic.vue
118 lines (107 loc) · 3.37 KB
/
Basic.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<template>
<Page>
<ActionBar>
<Label text="Basic GPS demo" />
</ActionBar>
<StackLayout>
<Label :text="message" class="message" textWrap="true" />
<ListView :items="gpsPoints">
<v-template>
<Label :text="item.name" />
</v-template>
</ListView>
</StackLayout>
</Page>
</template>
<script lang="ts">
import Vue from 'vue';
import { ObservableArray } from '@nativescript/core';
import { GPS } from '@nativescript-community/gps';
const gps = new GPS();
export default Vue.extend({
mounted() {
this.gpsPoints = new ObservableArray([]);
gps.on(GPS.gps_status_event, (e) => {
console.log('gps_status_event', e['data']);
});
this.enableLocation()
.then(() => {
console.log('enableLocation done');
gps.watchLocation(this.locationReceived.bind(this), this.error, {
provider: 'gps',
minimumUpdateTime: 1000
}).then((watchId) => (this.watchId = watchId));
})
.catch(this.error);
},
destroyed() {
if (this.watchId) {
console.log('clearWatch');
gps.clearWatch(this.watchId);
}
},
data() {
return {
watchId: null,
message: 'Tracking location',
gpsPoints: []
};
},
methods: {
enableLocation() {
if (!gps.isEnabled()) {
console.log('Location not enabled, requesting.');
return gps.authorize(true).then(() => gps.enable());
} else {
return Promise.resolve(true);
}
},
getLocation() {
if (gps.isEnabled()) {
return gps.getCurrentLocation({
minimumUpdateTime: 1000
});
}
return Promise.reject('Geolocation not enabled.');
},
locationReceived(position: any) {
console.log('GPS Update Received');
// {
// "latitude": 33.52077361638753,
// "longitude": -111.89930240833577,
// "altitude": 384.0000915527344,
// "horizontalAccuracy": 65,
// "verticalAccuracy": 10,
// "speed": -1,
// "direction": -1,
// "timestamp": "2016-10-04T00:22:59.316Z",
// "ios": {}
// }
console.log(JSON.stringify(position));
console.log(JSON.stringify(position.latitude));
console.log(JSON.stringify(position.longitude));
const gpsTime: Date = new Date(position.timestamp);
const logTime: Date = new Date();
const difference: number = (logTime.getTime() - gpsTime.getTime()) / 1000;
this.message = `last location:${difference},${position.latitude},${position.longitude}`;
this.gpsPoints.unshift({ name: gpsTime });
},
error(err) {
alert(err);
},
formatDate(date: Date) {
return date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
}
}
});
</script>
<style scoped lang="scss">
ActionBar {
background-color: #42b883;
color: white;
}
Button {
background-color: #42b883;
color: white;
}
</style>