-
Notifications
You must be signed in to change notification settings - Fork 9
/
run-geolocation.html
216 lines (197 loc) · 6.32 KB
/
run-geolocation.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
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Realtime Positioning</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox.js/v2.2.1/mapbox.js"></script>
<link href="https://api.mapbox.com/mapbox.js/v2.2.1/mapbox.css" rel="stylesheet" />
<style>
body {
margin:0;
padding:0;
}
#map {
position: absolute;
top:0;
bottom:0;
width:100%;
}
#info_box {
font-family: monospace, sans-serif;
position: absolute;
top:0;
right:0;
padding: 10px;
background-color:
#fff; opacity: 0.5;
}
#note_box {
font-family: monospace, sans-serif;
margin-top: 150px;
padding: 10px;
margin-left: auto;
margin-right: auto;
max-width: 80%;
background-color: #E9FBE9;
color: gray;
}
</style>
<script>
function map(element_id, info_box_id, token) {
var marker = null;
var map = null;
var MIN_CIRCLE_SIZE = 10;
L.mapbox.accessToken = token;
var updateInfoBox = infoBox(info_box_id);
function resolution(latitude, zoomLevel) {
var EARTH_CIRC = 6378137;
return Math.cos(latitude * Math.PI/180) * 2 * Math.PI * EARTH_CIRC / (256 * Math.pow(2, zoomLevel));
}
function circleRadius(accuracy, latitude, zoomLevel) {
var res = resolution(latitude, zoomLevel)
return Math.max(accuracy / res, MIN_CIRCLE_SIZE);
}
return function updateMap(err, coords, accuracy) {
var ZOOM_LEVEL = 17;
updateInfoBox(err, coords, accuracy);
if (err) {
if (marker) {
marker.setStyle({ color: "#999" });
}
} else {
if (map) {
map.panTo(coords);
} else {
map = L.mapbox.map(element_id, 'mapbox.streets').setView(coords, ZOOM_LEVEL);
map.on("zoomend", function() {
if (marker) {
var r = circleRadius(accuracy, coords[0], map.getZoom());
marker.setRadius(r);
}
});
}
if (marker) {
marker.setLatLng(coords);
} else {
marker = L.circleMarker(coords)
map.addLayer(marker);
}
var r = circleRadius(accuracy, coords[0], map.getZoom());
marker.setRadius(r);
marker.setStyle({ color: "#03f" });
}
};
}
function infoBox(element_id) {
var update_count = 0;
var last_updated;
function updateInfoBox(err, coords, accuracy) {
last_updated = Date.now();
update_count++;
var lines = [];
lines.push("Lat: " + (coords ? coords[0] : "…"));
lines.push("Lng: " + (coords ? coords[1] : "…"));
lines.push("Accuracy: " + (accuracy ? accuracy + "m" : "…"));
lines.push(err ? "Error: " + err.message : "");
lines.push("Last update: <span id=last_update>" + displayTimeInterval() + "</span> ago");
lines.push("Update count: " + update_count);
lines.push("<a style='float: right;' href=''>refresh</a>");
document.getElementById(element_id).innerHTML = lines.join("<br>");
}
function updateTimer() {
if (last_updated) {
document.getElementById("last_update").innerHTML = displayTimeInterval(Date.now() - last_updated);
}
requestAnimationFrame(updateTimer);
}
function displayTimeInterval() {
var delta = Date.now() - last_updated
return Math.floor(delta / 1000) + "." + pad(3, delta % 1000) + "s";
}
function pad(length, n) {
n = n + "";
while(n.length < length) {
n = "0" + n;
}
return n;
}
updateTimer();
return updateInfoBox;
}
const updateMap = map('map', 'info_box', 'pk.eyJ1IjoidG9iaWUiLCJhIjoiZDM5NDhkZWFkYjRjNTRlZTc4M2M0NDQ3ZmViMjgwNDUifQ.mtE579d9kCr58yG0sfx7vA');
</script>
<script type="module">
import { GeolocationSensor } from './src/geolocation-sensor.js';
import { AbortController, AbortSignal } from './src/sensor.js';
window.GeolocationSensor = GeolocationSensor;
const sensor = new GeolocationSensor({ accuracy: "low" });
sensor.onreading = () => {
const coords = [sensor.latitude, sensor.longitude];
updateMap(null, coords, sensor.accuracy);
};
sensor.onerror = (error) => {
updateMap(error);
};
let log = console.error;
console.error = (message, ...rest) => {
const div = document.querySelector('#console');
let child = document.createElement('div');
child.innerText = message;
div.appendChild(child);
log.call(console, message, ...rest);
}
window._start = function(target) {
if (!sensor.activated) {
target.innerText = "Stop";
sensor.start();
} else {
target.innerText = "Start";
sensor.stop();
}
}
let controller = null;
window._once = async function(target) {
if (controller) {
controller.abort();
target.innerText = "OneShot";
controller = null;
return;
}
target.innerText = "Cancel OneShot";
controller = new AbortController;
const signal = controller.signal;
try {
const value = await GeolocationSensor.read({ signal });
console.log(value);
target.innerText = "OneShot";
controller = null;
} catch(err) {
console.error(err);
target.innerText = "OneShot";
controller = null;
}
}
</script>
</head>
<body>
<div id="note_box">
<p>
We're using the navigator.permissions.query() API to find out
whether you allow this site to access your location. Use your
browser UI to grant location access to this site if you wish
to experience this example.
</p>
<p>
Chrome: Secure badge > Location > Always allow on this site
</p>
</div>
<div id="map"></div>
<p id="info_box">
<a href="">refresh</a>
<button onclick="_start(this)">Start</button>
<button onclick="_once(this)">OneShot</button>
</p>
<div id="console"></div>
</body>
</html>