-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
60 lines (52 loc) · 1.89 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
<!DOCTYPE html>
<html>
<title> </title>
<head>
<style>
#compass {
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
width: 300px;
height: 300px;
}
#needle {
position: absolute;
top: -2px;
left: 13px;
z-index: 10;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="jqRotate/jqRotate.js"></script>
</head>
<body>
<img src="compass.jpg" id="compass">
<img src="compassneedle.png" id="needle">
</body>
</html>
<script>
document.addEventListener("deviceready", onDeviceReady, false); //creates a listener that waits for the deviceready event and then fires onDevice ready function
function onDeviceReady() {
startCompass(); //runs the startCompass function
}
function startCompass() {
alert("reday");
var options = {
frequency : 50 //sets the compass heading to be updated every 50 milliseconds
};
navigator.compass.watchHeading(onSuccess, onError, options); //gets the compass heading from device and passes heading to onSuccess function
}
function onSuccess(heading) {//device s magnetic heading is passed to heading variable
$("#needle").rotate(-heading.magneticHeading); //jquery function that gets the #needle id and rotates it minus the current heading
}
function onError(compassError) { //if there is an error the error will be passed to onError in the compassError variable
alert('Compass error: ' + compassError.code); //displays an alert with the error code
}
</script>