方法:
compass.getCurrentHeading
compass.watchHeading
compass.clearWatch
参数:
compassSuccess
compassError
compassOptions
compass.getCurrentHeading
获取罗盘的当前朝向。
简单的范例:
复制navigator.compass.getCurrentHeading(compassSuccess, compassError, compassOptions);
1.
说明:
罗盘是一个检测设备方向或朝向的传感器,使用度作为衡量单位,取值范围从0度到359.99度。
通过compassSuccess回调函数返回罗盘朝向数据
支持的平台:
Android
iPhone
简单的范例:
复制function onSuccess(heading) {
alert(Heading: + heading);
}
function onError() {
alert(onError!);
}
navigator.compass.getCurrentHeading(onSuccess, onError);
1.2.3.4.5.6.7.
完整的范例:
复制<!DOCTYPE html>
<html>
<head>
<title>Compass Example</title>
<scripttype="text/javascript"charset="utf-8"src="phonegap.js"></script>
<scripttype="text/javascript"charset="utf-8">
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
navigator.compass.getCurrentHeading(onSuccess, onError);
}
// onSuccess: 返回当前的朝向数据
function onSuccess(heading) {
alert(Heading: + heading);
}
// onError: 返回朝向数据失败
function onError() {
alert(onError!);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>getCurrentHeading</p>
</body>
</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.
compass.watchHeading
在固定的时间间隔获取罗盘朝向的角度。
复制var watchID = navigator.compass.watchHeading(compassSuccess,
compassError, [compassOptions]);
1.2.
说明:
罗盘是一个检测设备方向或朝向的传感器,使用度作为衡量单位,取值范围从0度到359.99度。
compass.watchHeading每隔固定时间就获取一次设备的当前朝向。每次取得朝向后,headingSuccess回调函数会被执行。通过compassOptions对象的frequency参数可以设定以毫秒为单位的时间间隔。
返回的watch ID是罗盘监视周期的引用,可以通过compass.clearWatch调用该watch ID以停止对罗盘的监视。
支持的平台:
Android
iPhone
简单的范例:
复制function onSuccess(heading) {
var element = document.getElementById(heading);
element.innerHTML = Heading: + heading;
}
function onError() {
alert(onError!);
}
var options = { frequency: 3000 }; //每隔3秒更新一次
var watchID = navigator.compass.watchHeading(onSuccess, onError, options);
1.2.3.4.5.6.7.8.9.
完整的范例:
复制<!DOCTYPE html>
<html>
<head>
<title>Compass Example</title>
<scripttype="text/javascript"charset="utf-8"src="phonegap.js"></script>
<scripttype="text/javascript"charset="utf-8">
// watchID 是当前“watchHeading”的引用
var watchID = null;
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
startWatch();
}
// 开始监视罗盘
function startWatch() {
// 每隔3秒钟更新一次罗盘的朝向信息
var options = { frequency: 3000 };
watchID = navigator.compass.watchHeading(onSuccess, onError, options);
}
// 停止监视罗盘
function stopWatch() {
if (watchID) {
navigator.compass.clearWatch(watchID);
watchID = null;
}
}
// onSuccess: 返回罗盘的当前朝向
function onSuccess(heading) {
var element = document.getElementById(heading);
element.innerHTML = Heading: + heading;
}
// onError: 获取罗盘朝向失败
function onError() {
alert(onError!);
}
</script>
</head>
<body>
<divid="heading">Waiting for heading...</div>
<buttononclick="startWatch();">Start Watching</button>
<buttononclick="stopWatch();">Stop Watching</button>
</body>
</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.
compass.clearWatch
停止watch ID参数指向的罗盘监视。
复制navigator.compass.clearWatch(watchID);
1.
watchID: 由compass.watchHeading返回的引用标示。
支持的平台:
Android
iPhone
简单的范例:
复制var watchID = navigator.compass.watchHeading(onSuccess, onError, options);
// ... 后继处理 ...
navigator.compass.clearWatch(watchID);
1.2.3.
完整的范例:
复制<!DOCTYPE html>
<html>
<head>
<title>Compass Example</title>
<scripttype="text/javascript"charset="utf-8"src="phonegap.js"></script>
<scripttype="text/javascript"charset="utf-8">
// watchID是当前 “watchHeading” 的引用
var watchID = null;
// 等待加载PhoneGap
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap加载完毕
function onDeviceReady() {
startWatch();
}
// 开始监视罗盘
function startWatch() {
// 每隔3秒钟更新一次罗盘数据
var options = { frequency: 3000 };
watchID = navigator.compass.watchHeading(onSuccess, onError, options);
}
// 停止监视罗盘
function stopWatch() {
if (watchID) {
navigator.compass.clearWatch(watchID);
watchID = null;
}
}
// onSuccess: 返回罗盘的当前朝向
function onSuccess(heading) {
var element = document.getElementById(heading);
element.innerHTML = Heading: + heading;
}
// onError: 获取罗盘朝向失败
function onError() {
alert(onError!);
}
</script>
</head>
<body>
<divid="heading">Waiting for heading...</div>
<buttononclick="startWatch();">Start Watching</button>
<buttononclick="stopWatch();">Stop Watching</button>
</body>
</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.
compassSuccess
提供罗盘朝向信息的onSuccess回调函数。
复制function(heading) {
// 进一步处理
}
1.2.3.
参数:
heading:在某一时刻的罗盘朝向角度 (从0度到359.99度之间)。(数字类型)
范例:
复制function onSuccess(heading) {
alert(Heading: + heading);
}
1.2.3.
compassError
罗盘方法的onError回调函数。
复制function() {
// 错误处理
}
1.2.3.
compassOptions
定制检索罗盘朝向的可选参数。
选项:
frequency:多少毫秒获取一次罗盘朝向。(数字类型)(默认值:100)
暂时没有评论,来抢沙发吧~