小桥流水丿小溪 2013-05-02
文章来自:http://blog.csdn.net/phonegapcn/article/details/6695518
获得该设备的当前朝向。
compass.getCurrentHeading:获取罗盘的当前朝向。
navigator.compass.getCurrentHeading(compassSuccess, compassError, compassOptions);
说明:
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> <h1>Example</h1> <p>getCurrentHeading</p> </body> </html>
compass.watchHeading:在固定的时间间隔获取罗盘朝向的角度.
var watchID = navigator.compass.watchHeading(compassSuccess, compassError, [compassOptions]);
说明:
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返回的引用标示。
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回调函数。
参数:
function onSuccess(heading) { alert('Heading: ' + heading); }compassError :罗盘方法的onError回调函数。
compassOptions |
选项: