如何使用计步器插件?

时间:2015-02-24 18:50:08

标签: javascript angularjs cordova ionic

我正在开发基于原生应用程序的项目。我必须在cordova中编写一个简单的计步器。我已将该插件下载到我的文件中,但我没有成功将所有信息都恢复 这是我的代码:



var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

app.initialize();

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Pedometer</title>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script>
        var successHandler = function (pedometerData) {
    // pedometerData.numberOfSteps;
    // pedometerData.distance;
    // pedometerData.floorsAscended;
    // pedometerData.floorsDescended;
};
         pedometer.startPedometerUpdates(successHandler, onError);
 
         pedometer.stopPedometerUpdates(successCallback, failureCallback);
 
 
 
 
        </script>
    </body>
</html>
&#13;
&#13;
&#13;

感谢您的帮助! ;)

1 个答案:

答案 0 :(得分:0)

根据the docs

  

平台和设备支持

     

仅限iOS 8+。甚至,并非所有设备都支持这些功能   使用iOS 8,请确保使用检查功能支持   功能

因此,您可以测试此代码是否支持该功能:

pedometer.isStepCountingAvailable(function(){
    console.log( "Pedometer step counting is available" );
}, function(){
    console.log( "Pedometer step counting is NOT available" );
});

然后您可以使用pedometer.isDistanceAvailable()pedometer.isFloorCountingAvailable

执行相同的操作