如何使用计步器插件

How to use the pedometer plugin?

本文关键字:插件 计步器 何使用      更新时间:2024-05-06

我正在开发一个基于本机应用程序的项目。我必须用科多瓦编码一个简单的计步器。我已经在我的文件中下载了插件,但我没有成功地获得所有信息这是我的代码:

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>

谢谢你的帮助!)

根据文档:

平台和设备支持

仅限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 执行相同的操作