-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
127 lines (112 loc) · 3.68 KB
/
script.js
File metadata and controls
127 lines (112 loc) · 3.68 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
import * as L from 'https://unpkg.com/leaflet@1.9.4/dist/leaflet-src.esm.js';
import { createApp } from 'https://unpkg.com/vue@3.4.21/dist/vue.esm-browser.prod.js';
import History from "./history.js";
const app = createApp({
data() {
return {
position: {},
points: [],
loading: true,
error: false,
errorMessage: "",
map: null,
marker: null,
accuracyCircle: null,
options: {
highAccuracy: true,
},
};
},
async mounted() {
this.history = new History(this.points);
try {
// Initialize map first
this.map = L.map("map", {
attributionControl: false,
maxZoom: 22
});
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxNativeZoom: 19,
maxZoom: 22
}).addTo(this.map);
this.map.invalidateSize();
this.marker = L.marker([0, 0]).addTo(this.map);
this.accuracyCircle = L.circle([0, 0], {
radius: 0,
className: 'accuracy-circle'
}).addTo(this.map);
// Check for permissions first
const permission = await navigator.permissions.query({ name: 'geolocation' });
if (permission.state === 'denied') {
throw new Error('Location permission denied');
}
// Get initial position
const position = await new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(resolve, reject, {
enableHighAccuracy: this.options.highAccuracy,
timeout: 5000,
maximumAge: 0
});
});
// Set initial position
await this.onPositionChange(position);
// Start watching position
this.refreshPosition();
} catch (err) {
console.error('Location error:', err);
this.error = true;
this.errorMessage = this.getErrorMessage(err);
this.loading = false;
}
},
methods: {
getErrorMessage(error) {
switch(error.code) {
case 1: return 'Location permission denied';
case 2: return 'Location unavailable';
case 3: return 'Location request timed out';
default: return 'Error getting location';
}
},
updateMapPosition(latitude, longitude, accuracy) {
this.marker.setLatLng({ lat: latitude, lng: longitude });
this.accuracyCircle.setLatLng({ lat: latitude, lng: longitude }).setRadius(accuracy);
this.map.setView({ lat: latitude, lng: longitude }, 20);
this.map.fitBounds(this.accuracyCircle.getBounds(), { maxZoom: 20 });
},
onPositionChange(position) {
const { latitude, longitude } = position.coords;
const latest = this.history.getLatest();
if (latest && latitude === latest.latitude && longitude === latest.longitude) {
console.log("Same position as latest point. Skipping.");
return;
}
const point = {
latitude,
longitude,
accuracy: position.coords.accuracy,
timestamp: new Date(position.timestamp).toLocaleString(),
};
this.history.add(point);
this.position = this.history.reversedWeightedMean();
this.updateMapPosition(this.position.latitude, this.position.longitude, this.position.accuracy);
this.loading = false;
this.error = false;
},
refreshPosition() {
navigator.geolocation.watchPosition(
(position) => this.onPositionChange(position),
(error) => {
console.error("Error getting location:", error);
this.error = true;
this.errorMessage = this.getErrorMessage(error);
},
{
enableHighAccuracy: this.options.highAccuracy,
maximumAge: 0,
}
);
},
},
});
app.mount("#app");