PhoneGap API帮助文档翻译—Compass(罗盘)

文章来自:http://blog.csdn.net/phonegapcn/article/details/6695518

获得该设备的当前朝向。

方法:
  • compass.getCurrentHeading
  • compass.watchHeading
  • compass.clearWatch
参数:
  • compassSuccess
  • compassError
  • compassOptions

compass.getCurrentHeading:获取罗盘的当前朝向。

navigator.compass.getCurrentHeading(compassSuccess, compassError, compassOptions);

 说明:

罗盘是一个检测设备方向或朝向的传感器,使用度作为衡量单位,取值范围从0度到359.99度。
通过compassSuccess回调函数返回罗盘朝向数据.
支持的平台:
  • Android
  • iPhone
简单的范例:
function onSuccess(heading) { alert('Heading: ' + heading); } 
function onError() { alert('onError!'); } 
navigator.compass.getCurrentHeading(onSuccess, onError);
 完整的范例:
<!DOCTYPE html> <html> <head> <SCRIPT type=text/javascript charset=utf-8 src="phonegap.js"></SCRIPT> <SCRIPT type=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>
&nbsp;<h1>Example</h1> 
<p>getCurrentHeading</p> 
</body> 
</html>

 compass.watchHeading:在固定的时间间隔获取罗盘朝向的角度.

var watchID = navigator.compass.watchHeading(compassSuccess, compassError, [compassOptions]);

 说明:

罗盘是一个检测设备方向或朝向的传感器,使用度作为衡量单位,取值范围从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);

 完整的范例:

<!DOCTYPE html>
<html>
<head>
<title>Compass Example</title>
	
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="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>
	<div id="heading">Waiting for heading...</div>
	<button onclick="startWatch();">Start Watching</button>
	<button onclick="stopWatch();">Stop Watching</button>
</body>
</html>

 compass.clearWatch:停止watch ID参数指向的罗盘监视.

navigator.compass.clearWatch(watchID);

 watchID: 由compass.watchHeading返回的引用标示。

支持的平台:
  • Android
  • iPhone
简单的范例:
var watchID = navigator.compass.watchHeading(onSuccess, onError, options);  
      
// ... 后继处理 ...  
      
navigator.compass.clearWatch(watchID);

 完整的范例:

<!DOCTYPE html>
<html>
<head>
<title>Compass Example</title>
	
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="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>
	<div id="heading">Waiting for heading...</div>
	<button onclick="startWatch();">Start Watching</button>
	<button onclick="stopWatch();">Stop Watching</button>
</body>
</html>

 compassSuccess:提供罗盘朝向信息的onSuccess回调函数。

参数:

  • heading:在某一时刻的罗盘朝向角度 (从0度到359.99度之间)。(数字类型)
范例:
function onSuccess(heading) { alert('Heading: ' + heading); }
 compassError :罗盘方法的onError回调函数。
compassOptions 
 
定制检索罗盘朝向的可选参数。

选项:

  • frequency:多少毫秒获取一次罗盘朝向。(数字类型)(默认值:100)

相关推荐