View
 

iPhone: Magnetometer use

Page history last edited by Shazron Abdullah 2 years, 1 month ago
    function roundNumber(num) {
      var dec = 3;
      var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
      return result;
    }
 function watchCompass() {
      debug.log("watchCompass");
      var suc = function(a){
        document.getElementById('compass_magnetic').innerHTML = roundNumber(a.magneticHeading);
        document.getElementById('compass_true').innerHTML = roundNumber(a.trueHeading);
        document.getElementById('compass_accuracy').innerHTML = roundNumber(a.headingAccuracy);
      };
      var fail = function(){};
      var opt = {};
      opt.frequency = 100;
      timer = navigator.compass.watchHeading(suc,fail,opt);
    }
        <dl id="compass-data">
          <dt>M:</dt><dd id="compass_magnetic"> </dd>
          <dt>T:</dt><dd id="compass_true"> </dd>
          <dt>A:</dt><dd id="compass_accuracy"> </dd>
        </dl>
        <a href="#" class="btn large" onclick="watchCompass();">Watch Compass</a>