-
Notifications
You must be signed in to change notification settings - Fork 6
/
example.html
44 lines (40 loc) · 1.83 KB
/
example.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
<!DOCTYPE html>
<html>
<head>
<title>GeolocationThrottle Example</title>
</head>
<body>
<h1>GeolocationThrottle Example</h1>
<p>
This example will print the lat/lng of the current location, at most once every 5 seconds.
<br><br>
<button id="start">Start (watchPosition)</button>
<button id="stop">Stop (clearWatch)</button>
</p>
<div id="error"></div>
<ul id="positions"></ul>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="geolocation-throttle.js"></script>
<script>
if(window.navigator && window.navigator.geolocation) {
var watchId = null;
$("#start").click(function() {
var startTime = new Date();
watchId = GeolocationThrottle.watchPosition(function(position) {
var timeEllapsed = (new Date() - startTime) / 1000.0;
$('<li>' + timeEllapsed + ' seconds: ' + position.coords.latitude + ', ' + position.coords.longitude + '</li>').appendTo($("#positions"));
}, function() {
$("#error").html("Could not get position. Have you allowed your browser and this website to check it?");
}, {
throttleTime: 5000, // here we specify that we want a geolocation callback at most once every 5 seconds
})
});
$("#stop").click(function() {
GeolocationThrottle.clearWatch(watchId);
});
} else {
$("#error").html("Your browser doesn't seem to support the geolocation API");
}
</script>
</body>
</html>