-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
207 lines (169 loc) · 6.95 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html>
<head>
<title>Track GPS Location</title>
<link rel="manifest" href="manifest.json">
<!-- UIkit CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.16.24/dist/css/uikit.min.css" />
<!-- UIkit JS -->
<script src="https://cdn.jsdelivr.net/npm/uikit@3.16.24/dist/js/uikit.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/uikit@3.16.24/dist/js/uikit-icons.min.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@6/turf.min.js"></script>
<style>
#map {
height: 300px;
width: 100%;
}
</style>
</head>
<body class="uk-padding">
<h1><input type="checkbox" id="locationCheckbox" onchange="toggleDiv(this, 'location')" checked><label
for="locationCheckbox">Track GPS Location <span uk-icon="icon: crosshairs"></span></label></h1>
<div id="location">Waiting for device location <div uk-spinner></div>
</div>
<h2><input type="checkbox" id="featureCheckbox" onchange="toggleDiv(this, 'features')"><label
for="featureCheckbox">Features</label>
</h2>
<table id="features" style="display:none">
<tr>
<th>Distance (m)</th>
<th>Name</th>
<th>Type</th>
</tr>
<tbody id="featureTable"></tbody>
</table>
<h2><input type="checkbox" id="mapCheckbox" onchange="toggleDiv(this, 'map')"><label for="mapCheckbox">Map</label>
</h2>
<div id="map" style="display:none"></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoicGxhbmVtYWQiLCJhIjoiemdYSVVLRSJ9.g3lbg_eN0kztmsfIPxa9MQ';
const map = new mapboxgl.Map({
container: 'map', // container ID
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/streets-v12', // style URL
zoom: 1,
hash: true
});
const geolocate =
new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
// When active the map will receive updates to the device's location as it changes.
trackUserLocation: true,
// Draw an arrow next to the location dot to indicate which direction the device is heading.
showUserHeading: true
})
// Add the control to the map.
map.addControl(geolocate);
geolocate.trigger()
map.on('load', () => {
geolocate.trigger();
})
function toggleDiv(checkbox, id) {
console.log(this)
// Get the div element with the id 'map'
const div = document.getElementById(id);
// Check if the checkbox is checked
if (checkbox.checked) {
// If checked, show the map div by setting its display property to 'block'
div.style.display = 'block';
} else {
// If not checked, hide the map div by setting its display property to 'none'
div.style.display = 'none';
}
map.resize()
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(updatePosition, showError);
} else {
document.getElementById("location").innerHTML = "Geolocation is not supported by this browser.";
}
}
function updatePosition(position) {
// console.log(position)
document.getElementById("location").innerHTML =
`<ul>
<li>Longitude (X): ${position.coords.longitude}</li>
<li>Latitude (Y): ${position.coords.latitude}</li>
<li>Accuracy: ${position.coords.accuracy}m</li>
</ul>
`;
const checkbox = document.getElementById('locationCheckbox');
checkbox.checked = true
checkbox.onchange()
getLocationFeatures(position)
writePosition(position)
}
function getLocationFeatures(position) {
// Map query using https://docs.mapbox.com/playground/tilequery/
const queryURL = `https://api.mapbox.com/v4/mapbox.mapbox-streets-v8/tilequery/${position.coords.longitude},${position.coords.latitude}.json?radius=1000&limit=50&dedupe=true&geometry=point&access_token=pk.eyJ1IjoicGxhbmVtYWQiLCJhIjoiemdYSVVLRSJ9.g3lbg_eN0kztmsfIPxa9MQ`
fetch(queryURL)
.then(resp => resp.json())
.then(data => {
var features = data.features;
console.log(data.features)
var table = document.getElementById("featureTable");
table.innerHTML = ""
for (var i = 0; i < features.length; i++) {
console.log(features[i].properties.name || features[i].properties.house_num || "")
var name = features[i].properties.name || features[i].properties.house_num || "";
var featureClass = (features[i].properties.tilequery.layer || "") + '/' + (features[i].properties.class || "") + '/' + (features[i].properties.category_en || "");
var distance = features[i].properties.tilequery.distance || "";
var row = table.insertRow();
var distanceCell = row.insertCell();
var nameCell = row.insertCell();
var classCell = row.insertCell();
distanceCell.innerHTML = parseInt(distance);
nameCell.innerHTML = name;
classCell.innerHTML = featureClass;
}
})
}
function writePosition(position) {
// https://code.google.com/archive/p/google-mobwrite/wikis/Protocol.wiki
// https://textb.org/t/42rjtu8eel/
const message = `u:a585m_d10
F:1:42rjtu8eel
R:1:${position.coords.longitude},${position.coords.latitude},${position.coords.bearing}`
fetch("https://textb.org/mobwrite/", {
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/x-www-form-urlencoded",
},
"referrer": "https://textb.org/t/42rjtu8eel/",
"body": "q=" + encodeURIComponent(message) + "%0A%0A",
"method": "POST",
"mode": "no-cors",
"credentials": "omit"
});
}
// q=u%3Aa585m_d7%0AF%3A1%3A42rjtu8eel%0Ad%3A1%3A%3D4%20%2Babcd
// q=u%3At7bqdrq2%0AF%3A12%3A42rjtu8eel%0Ad%3A12%3A%3D4%09%2Babcd%0A%0A
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
document.getElementById("location").innerHTML = "User denied the request for Geolocation.";
break;
case error.POSITION_UNAVAILABLE:
document.getElementById("location").innerHTML = "Location information is unavailable.";
break;
case error.TIMEOUT:
document.getElementById("location").innerHTML = "The request to get user location timed out.";
break;
case error.UNKNOWN_ERROR:
document.getElementById("location").innerHTML = "An unknown error occurred.";
break;
}
}
// Get the initial location when the page loads
getLocation();
// Update the location every second (1000 milliseconds)
setInterval(getLocation, 1000);
</script>
</body>
</html>