-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
144 lines (120 loc) · 4.74 KB
/
index.html
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<html>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1.5.1/dist/leaflet.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get your position</title>
<style>
#mapid {
padding-top: 56.25%;
width: 100%;
}
</style>
<div id="app">
<div class="container">
<div class="row justify-content-center">
<div class="col">
<div class="card my-3">
<div class="card-header">Get your current position...</div>
<div class="card-body">
<div class="row" v-if="coord.latitude">
<div class="col">
<div class="form-group">
<label for="latitude">Latitude</label>
<input type="text" disabled id="latitude" class="form-control" :value="coord.latitude.toFixed(6)" />
</div>
</div>
<div class="col">
<div class="form-group">
<label for="longitude">Longitude</label>
<input type="text" disabled id="longitude" class="form-control" :value="coord.longitude.toFixed(6)" />
</div>
</div>
<div class="col">
<div class="form-group">
<label for="accuracy">Accuracy (m)</label>
<input type="text" disabled id="accuracy" class="form-control" :value="coord.accuracy.toFixed(2)" />
</div>
</div>
</div>
<p class="mb-0">
<button class="btn btn-secondary" @click="getLocation">Get location once...</button>
<button class="btn btn-primary" @click="followLocation(true)" v-if="!follow">Improve location...</button>
<button class="btn btn-danger" @click="followLocation(false)" v-else>Stop improving location...</button>
</p>
</div>
<div class="card-footer text-muted">{{ message }}</div>
</div>
<div class="card" v-if="coord.latitude">
<div id="mapid" class="card-img"></div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/combine/npm/vue@2.6.10,npm/jquery@3.4.1,npm/bootstrap@4.3.1/dist/js/bootstrap.min.js,npm/leaflet@1.5.1/dist/leaflet.min.js"></script>
<script>
(() => {
window.vue = new Vue({
data: {
coord: {},
follow: false,
map: null,
marker: null,
message: 'Click button to start...',
options: {
enableHighAccuracy: true,
timeout: 30000,
maximumAge: 0,
},
watch: null,
},
el: '#app',
methods: {
error(err) {
this.message = `ERROR(${err.code}): ${err.message}`
},
followLocation(follow) {
if (this.watch) {
navigator.geolocation.clearWatch(this.watch)
}
this.follow = follow
if (follow) {
this.coord.accuracy = 10000
this.watch = navigator.geolocation.watchPosition(this.narrow, this.error, this.options)
}
},
getLocation() {
navigator.geolocation.getCurrentPosition(this.success, this.error, this.options);
this.message = 'Location query started...'
},
narrow(pos) {
if (this.coord.latitude && pos.coords.accuracy >= this.coord.accuracy) {
// More inaccurate, drop result
this.message = "Received result, too inaccurate..."
return
}
this.success(pos)
},
success(pos) {
this.coord = pos.coords
this.message = 'Location query succeeded.'
window.setTimeout(() => {
if (!this.map) {
this.map = L.map('mapid').setView([this.coord.latitude, this.coord.longitude], 15)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors',
maxZoom: 18,
minZoom: 8,
}).addTo(this.map)
this.marker = L.marker([this.coord.latitude, this.coord.longitude]).addTo(this.map)
} else {
this.map.setView([this.coord.latitude, this.coord.longitude], 15)
this.marker.setLatLng([this.coord.latitude, this.coord.longitude])
}
}, 200)
},
},
})
})()
</script>
</html>