Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- document.addEventListener("deviceready", function(){
- $(document).ready(function() {
- console.log('applciaiton.js loaded.');
- // GLOBAL APP NAMESPACE FUNCTIONS
- app = {
- update_gps_accuracy: function(){
- function onSuccess(position) {
- acc = position.coords.accuracy;
- if (acc <= 100) {
- gps_sig = 'good';
- } else if (acc <= 500) {
- gps_sig = 'average';
- } else {
- gps_sig = 'poor';
- }
- $('.gps-signal').each(function(){
- if($(this).hasClass('right-button')) {
- console.log('This GPS tag is green.');
- gps_color = 'green';
- } else {
- gps_color = '';
- }
- bg_image_url = "url('images/gps/"+gps_sig+"gps"+gps_color+".png')";
- $(this).css('background-image', bg_image_url);
- });
- console.log('GPS accuracy updated.');
- };
- function onError(error) {
- // no sig
- // $('.gps-signal').html('No Sig');
- console.log('GPS signal unavailable.');
- $('.gps-signal').css('background-image','url("images/gps/nogps.png")');
- }
- navigator.geolocation.getCurrentPosition(onSuccess, onError);
- },
- watch_update_gps_accuracy: function(times, interval) {
- // sets default vars
- times = typeof times !== 'undefined' ? times : 5;
- interval = typeof interval !== 'undefined' ? interval : 10;
- console.log('Updating GPS accuracy every '+interval+' seconds, '+times+' times.' );
- time = 0;
- check_gps_int = setInterval(function(){
- if (time >= times) {
- clearInterval(check_gps_int);
- };
- app.update_gps_accuracy();
- time ++;
- }, interval*1000);
- },
- start_slidenation: function() {
- // Start slidenation
- slide_count = $('.slide').length
- for(i=1;i<=slide_count;i++) {
- $('.slidenation').append('<div></div>');
- }
- $('.slidenation div:first-child').addClass('active');
- },
- cb_slide_change: function(args) {
- // Slider change callback for slidenation
- $('.slidenation .active').removeClass('active');
- $('.slidenation div:nth-child('+args.currentSlideNumber+')').addClass('active');
- console.log('Slider changed. Current slide: '+args.currentSlideNumber);
- },
- start_walk: function() {
- // find out which walk this is
- // init local current_walk data
- // alert(db.next_walk_num());
- // start countup timer
- counter.start();
- // start watching position
- map.start_tracking();
- // on success update:
- // distance and pace
- // push cords
- // add new line on gmaps
- // on error
- // log error
- // if more than 5 errors?
- // stop tracking and alert
- },
- stop_walk: function() {
- // TODO
- // figure this out
- // for some reason we have to put the code in here,
- // as the map object becomes unavilable for some reason?
- // navigator.geolocation.clearWatch(watch_id);
- // console.log('Watching stopped.');
- // counter.stop();
- // console.log('Counter stopped.');
- map.stop_tracking();
- },
- pause_walk: function() {
- // TODO
- // figure this out
- // for some reason we have to put the code in here,
- // as the map object becomes unavilable for some reason?
- navigator.geolocation.clearWatch(watch_id);
- console.log('Watching stopped.');
- counter.stop();
- console.log('Counter stopped.');
- },
- resume_walk: function(){
- app.start_walk();
- }
- } // end of app.
- counter = {
- start: function() {
- counter.interval = setInterval(function(){
- if (counter.seconds >= 599) {
- clearInterval(counter.interval);
- alert('Max walk length');
- // TODO
- // make app stop walk
- return;
- }
- seconds = (counter.total_seconds%60).toString();
- // alert(seconds);
- // alert(seconds.length);
- if(seconds.length <= 1){
- seconds = '0'+ seconds;
- }
- $('.minutes').html(parseInt(counter.total_seconds/60));
- $('.seconds').html(seconds);
- counter.total_seconds ++;
- },1000);
- },
- total_seconds: 1,
- reset: function(){
- counter.total_seconds = 1;
- },
- stop: function(){
- clearInterval(counter.interval);
- },
- } // End of counter
- map = {
- stop_tracking: function(){
- navigator.geolocation.clearWatch(watch_id);
- console.log('Tracking has been stopped.');
- // TODO
- // stop timer
- // ask to save or discard
- },
- start_tracking: function(){
- // setup total walk vars
- var path_pos = [];
- var path = [];
- var polyline_cords = [];
- function onSuccess(position) {
- if (map.tracking) {
- console.log('stop tracking');
- };
- console.log('Latlng returned.');
- // useful vars
- current_loc = position;
- current_lat = position.coords.latitude;
- current_lng = position.coords.longitude;
- loc_text = 'Latitude: ' + position.coords.latitude + '<br />' +
- 'Longitude: ' + position.coords.longitude + '<br />';
- var myLatLng = new google.maps.LatLng(current_lat, current_lng);
- // first time in func, setup google map
- if (typeof prev_loc == 'undefined') {
- console.log('First time in tracking, building map...');
- // Google Map options
- // TODO
- // change the map options to remove all the buttons
- // on the scren
- var myOptions = {
- zoom: 17,
- center: myLatLng,
- mapTypeId: google.maps.MapTypeId.ROADMAP,
- disableDefaultUI: true,
- };
- // Create the Google Map, set options
- map = new google.maps.Map(document.getElementById("canvas"), myOptions);
- // drop in a marker here (with label?)
- var marker = new google.maps.Marker({
- position: myLatLng,
- map: map,
- // title: 'Hello World!'
- });
- // set the start of the polyline cords
- path.push(myLatLng);
- path_pos.push(position);
- console.log('Pushed initial cords to path.');
- console.log('Init prev_loc with current_loc.');
- prev_loc = current_loc;
- } else {
- // map already drawn, see if we nede to create a line
- prev_lat = prev_loc.coords.latitude;
- prev_lng = prev_loc.coords.longitude;
- prev_LatLng = new google.maps.LatLng(prev_lat,prev_lng);
- console.log('Map already drawn, test if cords have changed');
- if ( (parseFloat(prev_lat.toFixed(5)) != parseFloat(current_lat.toFixed(5)) ) || ( parseFloat(prev_lng.toFixed(5)) != parseFloat(current_lng.toFixed(5)) ) ) {
- console.log('Coords have changed.');
- // TODO
- // check to see if lat and lng taken down to a certain
- // decimal are the same, to stop minute change in gps
- // from redrawing
- // add cords to array
- path_pos.push(current_loc);
- path.push(myLatLng);
- // crate new polyline cords
- polyline_cords[0] = prev_LatLng;
- polyline_cords[1] = myLatLng;
- // draw new line segment
- console.log('Draw polyline.');
- var polyline = new google.maps.Polyline({
- path: polyline_cords,
- strokeColor: '#FF0000',
- strokeOpacity: 0.8,
- strokeWeight: 2
- });
- polyline.setMap(map);
- $('.slide').last().append(polyline_cords[0]+polyline_cords[1]);
- // set the new prev-cords
- console.log('Update the prev_loc var.');
- prev_loc = position;
- // TODO
- // update distance and pace
- }; // end coords changed
- } // end map already drawn
- } //end success
- function onError(error) {
- if (error.code == 3) {
- // code 3 is no pos change
- console.log('Watching stopped. Have not moved in 30 seconds.');
- } else {
- alert('code: ' + error.code + '\n' +
- 'message: ' + error.message + '\n');
- }
- console.log('GPS signal updating stopped.');
- clearInterval(gps_update);
- }
- // start the actual tracking
- console.log('Tracking initialised.');
- watch_id = navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: true });
- },
- tracking: true,
- } // end of map.
- db = {
- s: function(key, value) {
- window.localStorage.setItem(key, value);
- },
- g: function(key) {
- window.localStorage.getItem(key);
- },
- next_walk_num: function(){
- // check to see if there have been any previous walks
- if(db.g("next_walk_num") == null) {
- return 0;
- } else {
- return db.g("next_walk_num");
- }
- },
- } // end of db.
- // END DEVICE READY
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement