我在phonegap 3.1上有一个罗盘插件的大问题,
这是我的js代码的一部分:
var compass = {
inicialize:function(){
var options = {
frequency: 3000
};
var watchID = navigator.compass.watchHeading(compass.showInformation, function(err){console.log('Error',err)},options);
},
showInformation:function(heading){
alert('Heading',heading.magneticHeading);
console.log('Show Heading',heading);
$('#compass').html(heading.magneticHeading);
}
}
我已经在config.xml上添加了插件:
我运行adb logcat,这是我的日志:
D/CordovaWebView( 1209): CordovaWebView is running on device made by: unknown
I/Process ( 80): Sending signal. PID: 1209 SIG: 3
I/dalvikvm( 1209): threadid=3: reacting to signal 3
I/dalvikvm( 1209): Wrote stack traces to '/data/anr/traces.txt'
D/JsMessageQueue( 1209): Set native->JS mode to 2
I/Process ( 80): Sending signal. PID: 1209 SIG: 3
I/dalvikvm( 1209): threadid=3: reacting to signal 3
I/dalvikvm( 1209): Wrote stack traces to '/data/anr/traces.txt'
D/dalvikvm( 1209): GC_CONCURRENT freed 291K, 3% free 13533K/13895K, paused 10ms+
11ms
I/Process ( 80): Sending signal. PID: 1209 SIG: 3
I/dalvikvm( 1209): threadid=3: reacting to signal 3
I/dalvikvm( 1209): Wrote stack traces to '/data/anr/traces.txt'
D/webviewglue( 1209): nativeDestroy view: 0x1d4df0
I/ActivityManager( 80): Displayed com.coinding.mobile/.Coinding: +1s978ms
W/NetworkManagementSocketTagger( 80): setKernelCountSet(10025, 0) failed with
errno -2
**E/Web Console( 1209): Uncaught TypeError: Cannot call method 'watchHeading' of** u
ndefined at file:///android_asset/www/js/controllers/compass.js:6
I/Web Console( 1209): Geolocalization at file:///android_asset/www/js/init.js:25
答案 0 :(得分:0)
这可能是因为没有添加Compass插件。
要添加插件,请在根Cordova项目目录中运行:
cordova plugin add org.apache.cordova.device-orientation
这将通过在您的目录中生成相应的文件来修复有关navigator.compass
未定义的错误,但您仍需要添加适当的权限才能使此功能正常工作。
请参阅文档以获取更多详细信息: https://cordova.apache.org/docs/en/3.1.0/cordova_compass_compass.md.html#Compass 建议,每次加载Cordova在线文档,在阅读页面之前,请检查右上角的版本。