-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
165 lines (136 loc) · 3.97 KB
/
index.html
File metadata and controls
165 lines (136 loc) · 3.97 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8" />
<title>Geolocation watchPosition test</title>
<style>
body {
font-family: sans-serif;
margin: 20px;
}
button {
margin-right: 10px;
padding: 8px 12px;
}
#info {
margin-top: 10px;
font-size: 14px;
}
svg {
margin-top: 20px;
border: 1px solid #ccc;
width: 100%;
height: 400px;
background: #fafafa;
}
</style>
</head>
<body>
<button id="start">Start</button>
<button id="stop" disabled>Stop</button>
<button id="download" disabled>Pobierz CSV</button>
<div id="info">Brak danych</div>
<svg viewBox="0 0 1000 1000">
<polyline
id="path"
fill="none"
stroke="red"
stroke-width="3"
points=""
/>
</svg>
<script>
let watchId = null;
let points = [];
let minLat, maxLat, minLon, maxLon;
const info = document.getElementById("info");
const path = document.getElementById("path");
const downloadBtn = document.getElementById("download");
function updateBounds(lat, lon) {
if (minLat === undefined) {
minLat = maxLat = lat;
minLon = maxLon = lon;
} else {
minLat = Math.min(minLat, lat);
maxLat = Math.max(maxLat, lat);
minLon = Math.min(minLon, lon);
maxLon = Math.max(maxLon, lon);
}
}
function latLonToSvg(lat, lon) {
const padding = 20;
const width = 1000 - padding * 2;
const height = 1000 - padding * 2;
const x = padding + ((lon - minLon) / (maxLon - minLon || 1)) * width;
const y = padding + (1 - (lat - minLat) / (maxLat - minLat || 1)) * height;
return `${x},${y}`;
}
function downloadCSV() {
if (!points.length) return;
const header = "timestamp,latitude,longitude,accuracy\n";
const rows = points.map(p =>
`${p.timestamp},${p.latitude},${p.longitude},${p.accuracy}`
).join("\n");
const blob = new Blob([header + rows], { type: "text/csv" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "geolocation.csv";
a.click();
URL.revokeObjectURL(url);
}
document.getElementById("start").onclick = () => {
if (!navigator.geolocation) {
alert("Geolocation nie jest wspierane");
return;
}
points = [];
minLat = maxLat = minLon = maxLon = undefined;
path.setAttribute("points", "");
downloadBtn.disabled = true;
watchId = navigator.geolocation.watchPosition(
pos => {
const { latitude, longitude, accuracy } = pos.coords;
updateBounds(latitude, longitude);
points.push({
timestamp: pos.timestamp,
latitude,
longitude,
accuracy
});
const svgPoints = points.map(p =>
latLonToSvg(p.latitude, p.longitude)
);
path.setAttribute("points", svgPoints.join(" "));
info.textContent =
`lat: ${latitude.toFixed(6)}, ` +
`lon: ${longitude.toFixed(6)}, ` +
`accuracy: ±${accuracy.toFixed(1)} m, ` +
`punkty: ${points.length}`;
},
err => {
info.textContent = "Błąd: " + err.message;
},
{
enableHighAccuracy: true,
maximumAge: 0,
timeout: 10000
}
);
document.getElementById("start").disabled = true;
document.getElementById("stop").disabled = false;
};
document.getElementById("stop").onclick = () => {
if (watchId !== null) {
navigator.geolocation.clearWatch(watchId);
watchId = null;
}
document.getElementById("start").disabled = false;
document.getElementById("stop").disabled = true;
downloadBtn.disabled = points.length === 0;
info.textContent += " (zatrzymano)";
};
downloadBtn.onclick = downloadCSV;
</script>
</body>
</html>