简体   繁体   中英

Trying to build a Chrome Cordova App, but navigator is undefined

I'm building a Chrome Cordova App, and everything in the Chrome App APIs works just fine, but I'm trying to do some more advanced things, such as take a picture or read accelerometer data.

My app works just fine, but navigator seems to be undefined.

Here's a snippet of the code where I'm trying to access the accelerometer:

 $scope.picture = function(){navigator.accelerometer.getCurrentAcceleration( function (acceleration) {
            alert('Acceleration X: ' + acceleration.x + '\n' +
                  'Acceleration Y: ' + acceleration.y + '\n' +
                  'Acceleration Z: ' + acceleration.z + '\n' +
              'Timestamp: '      + acceleration.timestamp + '\n');
        },function () {}); }
    });

Here's some of the relevant logcat information:

D/CordovaLog(17498): chrome-extension://klfmkipmoapfodoemajgpobgjngbdejp/angular
.min.js: Line 86 : TypeError: Cannot call method 'getCurrentAcceleration' of und
efined
D/CordovaLog(17498):     at h.$scope.picture (chrome-extension://klfmkipmoapfodo
emajgpobgjngbdejp/controllers.js:31:53)
D/CordovaLog(17498):     at chrome-extension://klfmkipmoapfodoemajgpobgjngbdejp/
angular.min.js:166:92
D/CordovaLog(17498):     at chrome-extension://klfmkipmoapfodoemajgpobgjngbdejp/
angular.min.js:183:83
D/CordovaLog(17498):     at h.$eval (chrome-extension://klfmkipmoapfodoemajgpobg
jngbdejp/angular.min.js:104:370)
D/CordovaLog(17498):     at h.$apply (chrome-extension://klfmkipmoapfodoemajgpob
gjngbdejp/angular.min.js:105:121)
D/CordovaLog(17498):     at HTMLAnchorElement.<anonymous> (chrome-extension://kl
fmkipmoapfodoemajgpobgjngbdejp/angular.min.js:183:65)
D/CordovaLog(17498):     at HTMLAnchorElement.jQuery.event.dispatch (chrome-exte
nsion://klfmkipmoapfodoemajgpobgjngbdejp/jquery-1.9.1.js:3074:9)
D/CordovaLog(17498):     at HTMLAnchorElement.elemData.handle (chrome-extension:
//klfmkipmoapfodoemajgpobgjngbdejp/jquery-1.9.1.js:2750:28)
I/chromium(17498): [INFO:CONSOLE(86)] "TypeError: Cannot call method 'getCurrent
Acceleration' of undefined
I/chromium(17498):     at h.$scope.picture (chrome-extension://klfmkipmoapfodoem
ajgpobgjngbdejp/controllers.js:31:53)
I/chromium(17498):     at chrome-extension://klfmkipmoapfodoemajgpobgjngbdejp/an
gular.min.js:166:92
I/chromium(17498):     at chrome-extension://klfmkipmoapfodoemajgpobgjngbdejp/an
gular.min.js:183:83
I/chromium(17498):     at h.$eval (chrome-extension://klfmkipmoapfodoemajgpobgjn
gbdejp/angular.min.js:104:370)
I/chromium(17498):     at h.$apply (chrome-extension://klfmkipmoapfodoemajgpobgj
ngbdejp/angular.min.js:105:121)
I/chromium(17498):     at HTMLAnchorElement.<anonymous> (chrome-extension://klfm
kipmoapfodoemajgpobgjngbdejp/angular.min.js:183:65)
I/chromium(17498):     at HTMLAnchorElement.jQuery.event.dispatch (chrome-extens
ion://klfmkipmoapfodoemajgpobgjngbdejp/jquery-1.9.1.js:3074:9)
I/chromium(17498):     at HTMLAnchorElement.elemData.handle (chrome-extension://
klfmkipmoapfodoemajgpobgjngbdejp/jquery-1.9.1.js:2750:28)", source: chrome-exten
sion://klfmkipmoapfodoemajgpobgjngbdejp/angular.min.js (86)
I/CordovaLog(17498): Found start page location: plugins/org.chromium.bootstrap/c
hromeapp.html

I understand completely that I'll need to use a Shim eventually, as navigator will only work on mobile. I'm just trying to get the mobile use case working first.

Figured it out. (I've never used Cordova before). Apparently you have to install a plugin in order to get access to Cordova features. Now I know!

The fix was:

$ cordova plugin add org.apache.cordova.device-motion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM