Search code examples
androidtitaniumaccelerometeraccelerator

Test people standing still or motion in titanium


Now I want to do a app to test people standing still or motion in titanium..I use accelerometer and operated acceleration in 3 axis x,y,z..What I must use formula to test that problem..!


Solution

  • Another option would be to use GPS.

    var win = Ti.UI.createWindow({ backgroundColor: 'white' });
    
    // WARNING: This will only work well outside, where the phone can get a good GPS signal.
    var label = Ti.UI.createLabel({ text: 'Traveled 0 ft' });
    win.add(label);
    
    // Set up the geolocation code
    Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
    Ti.Geolocation.distanceFilter = 0.1;
    Ti.Geolocation.purpose = 'To track how far you have traveled!';
    var lastLocation = null, totalFtTraveled = 0;
    
    /**
     * This function is called by the phone every time we have new geolocation data. It writes out where the user currently is.
     * @param e An argument given to us by the phone with information such as accuracy, latitude, and longitude.
     */
    function reportPosition(e) {
        if (!e.success || e.error) {
            label.text = 'error: ' + JSON.stringify(e.error);
        }
        else {
            if (lastLocation != null) {
                var lat1 = lastLocation.latitude, lon1 = lastLocation.longitude;
                var lat2 = e.coords.latitude, lon2 = e.coords.longitude;
                var kmTraveled = 3963.0 * Math.acos(
                    Math.sin(lat1 / 57.2958) * Math.sin(lat2 / 57.2958)
                        + Math.cos(lat1 / 57.2958) * Math.cos(lat2 / 57.2958)
                        * Math.cos(lon2 / 57.2958 - lon1 / 57.2958)
                );
                var ftTraveled = kmTraveled * 3280.8399;
                totalFtTraveled += ftTraveled;
                label.text = 'Traveled ' + totalFtTraveled + 'ft';
            }
            lastLocation = e.coords;
        }
    }
    
    // This will get the location right now, and will get the phone ready to listen for the user's current location.
    Ti.Geolocation.getCurrentPosition(reportPosition);
    // And this fires whenever the "distance filter" is surpassed -- with a filter of 1, this happens every 1 meter or so.
    Ti.Geolocation.addEventListener('location', reportPosition);
    
    var reset = Ti.UI.createButton({ title: 'Reset', bottom: 10, height: 50, width: 200 });
    reset.addEventListener('click', function() {
        totalFtTraveled = 0;
        label.text = 'Traveled 0ft';
    });
    win.add(reset);
    
    win.open();