【发布时间】:2015-06-04 16:44:05
【问题描述】:
我正在尝试让指南针功能在我的应用程序上运行,但没有成功。从错误来看,我倾向于认为插件没有被添加或设备没有准备好。但据我所知,我已经做了这两件事。我正在使用 phonegap 构建,所以我用来包含它的唯一代码是:
<gap:plugin name="org.apache.cordova.device-orientation" version="0.3.9" />
据我所知,这就是所有需要的。当我在我的应用程序的 phonegap 构建页面中单击插件选项卡时,它会显示在那里。
watchDirection = null;
//Phonegap is ready
var whenDeviceReady = function(){
console.log("deviceready");
console.log(navigator.compass);
var findMyDirection = function(){
console.log("find my heading fired");
watchDirection = navigator.compass.watchHeading(
//onSuccess
function(heading){
console.log(heading);
var magnetDirection = heading.magneticHeading;
$("#movingCompass").css("transform", "rotate(" + magnetDirection + "deg)");
},
//onError
function(error){
console.log('code: ' + error.code +' message: ' + error.message);
},
//Update Frequency
{ frequency: 1000});
}
findMyDirection();
}
//Wait for phonegap to load
document.addEventListener("deviceready", whenDeviceReady, false);
上述代码按预期工作,但指南针对象除外。 console.log(navigator.compass); 返回为 undefined。并且行中有一个错误,其中包括 watchDirection = navigator.compass.watchHeading( 说 Uncaught TypeError: Cannot read property 'watchHeading' of undefined。
它被列在已安装插件的 phonegap 构建列表中,并且我已经使用我上面发布的相同代码格式进行地理定位。
【问题讨论】:
-
试一试:
<gap:plugin name="org.apache.cordova.device-orientation" source="plugins.cordova.io" />尽管他们不推荐该存储库,因为它将于 2015 年 10 月 EOL。更好的做法是使用 npm:<gap:plugin name="cordova-plugin-device-orientation" source="npm" /> -
不幸的是没有帮助!不过还是谢谢。
标签: android cordova phonegap-plugins phonegap-build