Phonegap 指南针插件无法正常工作
Phonegap compass plugin not working
我试图让罗盘功能在我的应用程序上运行,但没有成功。从错误来看,我倾向于认为没有添加插件或者设备没有准备好。但据我所知,这两件事我都做过。我正在使用 phonegap build,所以我用来包含它的唯一代码是:
<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);
returns 为undefined
。该行有一个错误,其中包括 watchDirection = navigator.compass.watchHeading(
说 Uncaught TypeError: Cannot read property 'watchHeading' of undefined
.
它被列在已安装插件的 phonegap 构建列表中,我已经有了使用我上面发布的相同代码格式的地理定位。
@Marty.H,我在这里有工作演示:http://codesnippets.altervista.org/examples/phonegap/demos/PUBLIC.Apps.html转到页面的下半部分并尝试预构建的应用程序,Phonegap-Compass-Test。如果有效,页面的上半部分有指向 github 的链接,您可以从中获取源代码。 --杰西
我试图让罗盘功能在我的应用程序上运行,但没有成功。从错误来看,我倾向于认为没有添加插件或者设备没有准备好。但据我所知,这两件事我都做过。我正在使用 phonegap build,所以我用来包含它的唯一代码是:
<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);
returns 为undefined
。该行有一个错误,其中包括 watchDirection = navigator.compass.watchHeading(
说 Uncaught TypeError: Cannot read property 'watchHeading' of undefined
.
它被列在已安装插件的 phonegap 构建列表中,我已经有了使用我上面发布的相同代码格式的地理定位。
@Marty.H,我在这里有工作演示:http://codesnippets.altervista.org/examples/phonegap/demos/PUBLIC.Apps.html转到页面的下半部分并尝试预构建的应用程序,Phonegap-Compass-Test。如果有效,页面的上半部分有指向 github 的链接,您可以从中获取源代码。 --杰西