facebook

navigator.accelerometer

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #345432 Reply

    etagski
    Member

    Hi, I’m trying to get the function navigator.accelerometer working.

    I saw there was an article posted on Wed Aug 28, 2013 but unfortunately I couldn’t get this to work. Whenever I inspect the code for navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);, the system says “Uncaught TypeError: Cannot call method ‘getCurrentAcceleration’ of undefined”.

    Should I be including an extra javascript file?

    I’ve also tried
    watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

    but get the same problem.

    The sample code is:

    // Start watching the acceleration //
    function startWatch()
    {
    watchID = navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess(acceleration)
    {
    var element = document.getElementById(‘m1-MobiOne-OutputStatus’);
    element.innerHTML = ‘Acceleration X: ‘ + acceleration.x + ‘<br />’ + ‘Acceleration Y: ‘ + acceleration.y + ‘<br />’ + ‘Acceleration Z: ‘ + acceleration.z + ‘<br />’ +
    ‘Timestamp: ‘ + acceleration.timestamp + ‘<br />’;
    }

    // onError: Failed to get the acceleration
    //
    function onError()
    {
    alert(‘onError – acceleration!’);
    }

    #345438 Reply

    Brandon
    Member

    Make sure you are waiting for the document to load before trying to use it.

    // Wait for device API libraries to load
    //
    document.addEventListener(“deviceready”, onDeviceReady, false);

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: navigator.accelerometer

You must be logged in to post in the forum log in