Advertisement
Guest User

application.js

a guest
Aug 30th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. document.addEventListener("deviceready", function(){
  3.  
  4.     $(document).ready(function() {
  5.         console.log('applciaiton.js loaded.');
  6.  
  7.  
  8.     // GLOBAL APP NAMESPACE FUNCTIONS
  9.         app = {
  10.             update_gps_accuracy: function(){
  11.                
  12.                 function onSuccess(position) {
  13.                     acc = position.coords.accuracy;
  14.                     if (acc <= 100) {
  15.                         gps_sig = 'good';
  16.                     } else if (acc <= 500) {
  17.                         gps_sig = 'average';
  18.                     } else {
  19.                         gps_sig = 'poor';
  20.                     }
  21.                     $('.gps-signal').each(function(){
  22.                         if($(this).hasClass('right-button')) {
  23.                             console.log('This GPS tag is green.');
  24.                             gps_color = 'green';
  25.                         } else {
  26.                             gps_color = '';
  27.                         }
  28.                         bg_image_url = "url('images/gps/"+gps_sig+"gps"+gps_color+".png')";
  29.                         $(this).css('background-image', bg_image_url);
  30.  
  31.                     });
  32.  
  33.  
  34.  
  35.                     console.log('GPS accuracy updated.');
  36.                 };
  37.  
  38.                 function onError(error) {
  39.                     // no sig
  40.                     // $('.gps-signal').html('No Sig');
  41.                     console.log('GPS signal unavailable.');
  42.                     $('.gps-signal').css('background-image','url("images/gps/nogps.png")');
  43.                 }
  44.  
  45.                 navigator.geolocation.getCurrentPosition(onSuccess, onError);      
  46.  
  47.             },
  48.             watch_update_gps_accuracy: function(times, interval) {
  49.                 // sets default vars
  50.                 times = typeof times !== 'undefined' ? times : 5;
  51.                 interval = typeof interval !== 'undefined' ? interval : 10;
  52.                 console.log('Updating GPS accuracy every '+interval+' seconds, '+times+' times.' );
  53.                 time = 0;
  54.                 check_gps_int = setInterval(function(){
  55.                     if (time >= times) {
  56.                         clearInterval(check_gps_int);
  57.                     };
  58.                     app.update_gps_accuracy();
  59.                     time ++;
  60.                 }, interval*1000); 
  61.             },
  62.             start_slidenation: function() {
  63.             // Start slidenation
  64.                 slide_count = $('.slide').length
  65.                 for(i=1;i<=slide_count;i++) {
  66.                     $('.slidenation').append('<div></div>');
  67.                 }
  68.                 $('.slidenation div:first-child').addClass('active');
  69.             },
  70.             cb_slide_change: function(args) {
  71.             // Slider change callback for slidenation
  72.                 $('.slidenation .active').removeClass('active');
  73.                 $('.slidenation div:nth-child('+args.currentSlideNumber+')').addClass('active');
  74.                 console.log('Slider changed. Current slide: '+args.currentSlideNumber);
  75.             },
  76.             start_walk: function() {
  77.  
  78.                 // find out which walk this is
  79.                     // init local current_walk data
  80.                 // alert(db.next_walk_num());
  81.  
  82.  
  83.                 // start countup timer
  84.                 counter.start();
  85.  
  86.                 // start watching position
  87.                 map.start_tracking();
  88.                     // on success update:
  89.                         // distance and pace
  90.                         // push cords
  91.                         // add new line on gmaps
  92.  
  93.                     // on error
  94.                         // log error
  95.                         // if more than 5 errors?
  96.                             // stop tracking and alert
  97.  
  98.  
  99.  
  100.  
  101.             },
  102.             stop_walk: function() {
  103.                 // TODO
  104.                 // figure this out
  105.                 // for some reason we have to put the code in here,
  106.                 // as the map object becomes unavilable for some reason?
  107.  
  108.                 // navigator.geolocation.clearWatch(watch_id);
  109.                 // console.log('Watching stopped.');
  110.  
  111.                 // counter.stop();
  112.                 // console.log('Counter stopped.');
  113.  
  114.                 map.stop_tracking();
  115.  
  116.  
  117.             },
  118.             pause_walk: function() {
  119.                 // TODO
  120.                 // figure this out
  121.                 // for some reason we have to put the code in here,
  122.                 // as the map object becomes unavilable for some reason?
  123.                 navigator.geolocation.clearWatch(watch_id);
  124.                 console.log('Watching stopped.');
  125.  
  126.                 counter.stop();
  127.                 console.log('Counter stopped.');
  128.  
  129.             },
  130.             resume_walk: function(){
  131.                 app.start_walk();
  132.             }
  133.  
  134.         } // end of app.
  135.  
  136.         counter = {
  137.             start: function() {
  138.                 counter.interval = setInterval(function(){
  139.                     if (counter.seconds >= 599) {
  140.                         clearInterval(counter.interval);
  141.                         alert('Max walk length');
  142.                         // TODO
  143.                         // make app stop walk
  144.                         return;
  145.                     }
  146.                     seconds = (counter.total_seconds%60).toString();
  147.                     // alert(seconds);
  148.                     // alert(seconds.length);
  149.                     if(seconds.length <= 1){
  150.                         seconds = '0'+ seconds;
  151.                     }
  152.                     $('.minutes').html(parseInt(counter.total_seconds/60));
  153.                     $('.seconds').html(seconds);   
  154.                     counter.total_seconds ++;
  155.                 },1000);
  156.  
  157.             },
  158.             total_seconds: 1,
  159.             reset: function(){
  160.                 counter.total_seconds = 1;
  161.             },
  162.             stop: function(){
  163.                 clearInterval(counter.interval);
  164.             },
  165.  
  166.         } // End of counter
  167.  
  168.  
  169.         map = {
  170.             stop_tracking: function(){
  171.                 navigator.geolocation.clearWatch(watch_id);
  172.                 console.log('Tracking has been stopped.');
  173.                
  174.                 // TODO
  175.                 // stop timer
  176.                 // ask to save or discard
  177.  
  178.             },
  179.             start_tracking: function(){
  180.                 // setup total walk vars
  181.                 var path_pos = [];
  182.                 var path = [];
  183.                 var polyline_cords = [];
  184.  
  185.                 function onSuccess(position) {
  186.  
  187.                     if (map.tracking) {
  188.                         console.log('stop tracking');
  189.  
  190.                     };
  191.  
  192.  
  193.                     console.log('Latlng returned.');
  194.                 // useful vars
  195.                     current_loc = position;
  196.                     current_lat = position.coords.latitude;
  197.                     current_lng = position.coords.longitude;
  198.                     loc_text =  'Latitude: '  + position.coords.latitude      + '<br />' +
  199.                             'Longitude: ' + position.coords.longitude     + '<br />';
  200.                     var myLatLng = new google.maps.LatLng(current_lat, current_lng);
  201.  
  202.                 // first time in func, setup google map
  203.                     if (typeof prev_loc == 'undefined') {
  204.                         console.log('First time in tracking, building map...');
  205.                         // Google Map options
  206.  
  207.                         // TODO
  208.                         // change the map options to remove all the buttons
  209.                         // on the scren
  210.  
  211.                         var myOptions = {
  212.                           zoom: 17,
  213.                           center: myLatLng,
  214.                           mapTypeId: google.maps.MapTypeId.ROADMAP,
  215.                           disableDefaultUI: true,
  216.                         };
  217.                         // Create the Google Map, set options
  218.                         map = new google.maps.Map(document.getElementById("canvas"), myOptions);
  219.  
  220.                         // drop in a marker here (with label?)
  221.                         var marker = new google.maps.Marker({
  222.                             position: myLatLng,
  223.                             map: map,
  224.                             // title: 'Hello World!'
  225.                         });
  226.  
  227.                         // set the start of the polyline cords
  228.                         path.push(myLatLng);
  229.                         path_pos.push(position);
  230.                         console.log('Pushed initial cords to path.');
  231.  
  232.                         console.log('Init prev_loc with current_loc.');
  233.                         prev_loc = current_loc;
  234.                     } else {
  235.                         // map already drawn, see if we nede to create a line
  236.                         prev_lat = prev_loc.coords.latitude;
  237.                         prev_lng = prev_loc.coords.longitude;
  238.                         prev_LatLng = new google.maps.LatLng(prev_lat,prev_lng);
  239.                         console.log('Map already drawn, test if cords have changed');
  240.                         if ( (parseFloat(prev_lat.toFixed(5)) != parseFloat(current_lat.toFixed(5)) ) || ( parseFloat(prev_lng.toFixed(5)) != parseFloat(current_lng.toFixed(5)) ) ) {
  241.                             console.log('Coords have changed.');
  242.  
  243.                             // TODO
  244.                             // check to see if lat and lng taken down to a certain
  245.                             // decimal are the same, to stop minute change in gps
  246.                             // from redrawing
  247.  
  248.                             // add cords to array
  249.                             path_pos.push(current_loc);
  250.                             path.push(myLatLng);
  251.                             // crate new polyline cords
  252.                             polyline_cords[0] = prev_LatLng;
  253.                             polyline_cords[1] = myLatLng;
  254.                             // draw new line segment
  255.                             console.log('Draw polyline.');
  256.                             var polyline = new google.maps.Polyline({
  257.                               path: polyline_cords,
  258.                               strokeColor: '#FF0000',
  259.                               strokeOpacity: 0.8,
  260.                               strokeWeight: 2
  261.                             });
  262.  
  263.                             polyline.setMap(map);
  264.  
  265.                             $('.slide').last().append(polyline_cords[0]+polyline_cords[1]);
  266.  
  267.                             // set the new prev-cords
  268.                             console.log('Update the prev_loc var.');
  269.                             prev_loc = position;                   
  270.  
  271.                             // TODO
  272.                             // update distance and pace
  273.  
  274.                         }; // end coords changed
  275.  
  276.                     } // end map already drawn
  277.  
  278.                 } //end success
  279.  
  280.                 function onError(error) {
  281.                     if (error.code == 3) {
  282.                         // code 3 is no pos change
  283.                         console.log('Watching stopped. Have not moved in 30 seconds.');
  284.                     } else {
  285.                         alert('code: '    + error.code    + '\n' +
  286.                               'message: ' + error.message + '\n');
  287.                     }
  288.                     console.log('GPS signal updating stopped.');
  289.                     clearInterval(gps_update);
  290.                 }
  291.  
  292.                 // start the actual tracking
  293.                 console.log('Tracking initialised.');
  294.                 watch_id = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: true });
  295.             },
  296.             tracking: true,
  297.  
  298.            
  299.         } // end of map.
  300.  
  301.  
  302.         db = {
  303.             s: function(key, value) {
  304.                 window.localStorage.setItem(key, value);
  305.             },
  306.             g: function(key) {
  307.                 window.localStorage.getItem(key);
  308.             },
  309.             next_walk_num: function(){
  310.                 // check to see if there have been any previous walks
  311.                 if(db.g("next_walk_num") == null) {
  312.                     return 0;
  313.                 } else {
  314.                     return db.g("next_walk_num");
  315.                 }
  316.             },
  317.  
  318.         } // end of db.
  319.  
  320.  
  321.  
  322.  
  323.  
  324.     // END DEVICE READY
  325.     });
  326.  
  327. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement