/* * Called to get distance and show the direction compass, this calls itself every 3seconds */ // ideally we'd use the standard watchPosition function instead of us calling getCurrentPosition // repeatedly. However its not implemented in the geo.js library we are using as some devices // dont support it. function showDistanceDirection(callback_func) { getCoords(callback_func); setTimeout("showDistanceDirection("+callback_func+")", 3000); // call function repeatedly to update values } /* * Tries to get GPS coordinates and call a callback function with them. */ function getCoords(success_cb, error_cb) { if(geo_position_js.init()){ if (typeof success_cb == 'undefined') return null; // use default if (typeof error_cb == 'undefined') error_cb = error_callback; geo_position_js.getCurrentPosition(success_cb, error_cb, {enableHighAccuracy:true}); } } /* * Default error callback for all GPS requests. */ function error_callback(p) { // ignore?? perhaps show message on screen instead of alert //alert('Error: '+p.message); } /* * Gets a distance string in either metric or imperial form */ function getDistanceStr(distance, is_metric) { var unit = 'km'; if(is_metric) { if(distance < 1) unit = 'm'; } else { unit = 'mi'; if(distance < 1.609344) // 1.609344 km == 1 mile unit = 'yrd' } distance = convertDistance(distance, unit); return distance + " " + unit; } function convertDistance(distance, unit) { var numDecimals = 1; distance = parseFloat(distance); switch(unit) { case 'm': distance = distance * 1000; numDecimals = 0; break; case 'mi': distance = distance / 1.609344; break; case 'yrd': distance = distance * 1093.6133; numDecimals = 0; break; default: break; } var pow = Math.pow(10,numDecimals); return Math.round(distance*pow)/pow; } /* * Convert a bearing in degrees to a textual representation */ function convertDegreesToDirection(degrees) { if(typeof degrees != 'number' || degrees < 0 || degrees > 360) return null; var directions = new Array("n", "ne", "e", "se", "s", "sw", "w", "nw"); var degreesBetween = 45; var startDegrees = 22.5; var direction = "n"; var min = startDegrees*-1; // start 22.5 back so the segment increases in 45deg segments var max = startDegrees; for(var i=0; i= min && degrees <= max) { direction = directions[i]; break; } min = max; max += degreesBetween; } return direction; } // save a cookie to indicate that we were in the success callback function and so // the user has given thier permission to use their location. // no idea how long a device will keep the user's permission to use their location // this cookie timeout is 1day. If this cookie is set and the permission is reset // then the device WILL show a permission dialog on the front screen. function saveInSuccessFunc() { setCookie("findus_success", "true", 1); } function checkForFindUsCookie() { return (getCookie("findus_success") != null); } function setCookie(name, value, days){ var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); expires = "; expires="+date.toGMTString(); } document.cookie = name+"="+value+expires+"; path=/"; } function getCookie(name) { var nameEQ = name + "="; var all_cookies = document.cookie.split(';'); for(var i=0; i<all_cookies.length;i++) { var c = all_cookies[i]; while (c.charAt(0)==' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; }
