var x = Math.sin(heading / 360 * Math.PI * 2) * 50 * -1;x方向をsin、y方向をcosで取得していますね。そして針の向きを補正するのにそれぞれ-1をかけています。また、headingの値をラジアンに変換するために、取得されたheadingの値を360で割り、Math.PI * 2をかけています。
var y = Math.cos(heading / 360 * Math.PI * 2) * 50 * -1;
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<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.0.1/jquery.mobile-1.0.1.min.css" />
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript"
src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
<script type="text/javascript" src="phonegap-1.4.1.js"></script>
<script type="text/javascript">
function onLoad(){
document.addEventListener("deviceready", onReady, false);
}
function draw(heading){
var canvas = document.getElementById('canvas1');
var context = canvas.getContext('2d');
context.fillStyle = "#EEEEFF";
context.fillRect(0, 0, 300, 200);
context.beginPath();
context.arc(100, 100, 50, 0, Math.PI * 2, true);
context.closePath();
context.fillStyle = 'rgba(255, 0, 0, 0.5)';
context.fill();
context.beginPath();
context.strokeStyle = 'red';
context.moveTo(100, 100);
var x = Math.sin(heading / 360 * Math.PI * 2) * 50 * -1;
var y = Math.cos(heading / 360 * Math.PI * 2) * 50 * -1;
context.lineTo(100 + x, 100 + y);
context.closePath();
context.stroke();
}
function onReady() {
var opt = { frequency: 200 };
navigator.compass.watchHeading(onSuccess, onError, opt);
}
function onSuccess(heading) {
var heading = heading.trueHeading;
document.getElementById('msg').innerHTML = "HEAD: " + heading;
draw(heading);
}
function onError(compassError) {
alert('Compass Error: ' + compassError.code);
}
</script>
</head>
<body onload="onLoad();">
<!-- home page -->
<div id="home" data-role="page">
<div data-role="header">
<h1>Sample</h1>
</div>
<div data-role="content">
<h2>Sample Page</h2>
<div id="msg"></div>
<canvas id="canvas1" width="300" height="200"></canvas>
</div>
</div>
</body>
</html>
| << 前へ |