Skip to content

Instantly share code, notes, and snippets.

@sarfata
Created May 29, 2014 03:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarfata/df39171bebee7766b816 to your computer and use it in GitHub Desktop.
Save sarfata/df39171bebee7766b816 to your computer and use it in GitHub Desktop.
JSConf'14 Scavenger Hunt app
var card = new Pebble.UI.Card({ title: "JSConf 2014", subtitle: "Scavenger Hunt", scrollable: true });
card.show();
var msg = "Contacting NSA to get your current position ";
var timer = setInterval(function() {
card.body(msg);
msg += ".";
}, 800);
function distanceBetweenCoordinates(pos1, pos2) {
var R = 6371; // Radius of the earth in km
var dLat = (pos2.latitude - pos1.latitude) * (Math.PI/180); // deg2rad below
var dLon = (pos2.longitude - pos1.longitude) * (Math.PI/180);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos((pos1.latitude) * (Math.PI/180)) * Math.cos(pos2.latitude * (Math.PI/180)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c * 1000; // Distance in m
return d;
}
function angleBetweenCoords(pos1, pos2) {
var dLon = (pos2.longitude - pos1.longitude);
var y = Math.sin(dLon) * Math.cos(pos2.latitude);
var x = Math.cos(pos1.latitude) * Math.sin(pos2.latitude) - Math.sin(pos1.latitude) * Math.cos(pos2.latitude) * Math.cos(dLon);
var brng = Math.atan2(y, x);
brng = brng * (180 / Math.PI);
brng = (brng + 360) % 360;
brng = 360 - brng;
return brng;
}
var cardinals = {
'N': [ 360 - 45/2, 360+45/2 ],
'NE': [ 45 - 45/2, 45 + 45/2 ],
'E' : [ 90 - 45/2, 90 + 45/2 ],
'SE': [ 135 - 45/2, 135 + 45/2 ],
'S': [ 180 - 45/2, 180 + 45/2 ],
'SW': [ 225 - 45/2, 225 + 45/2 ],
'W': [ 270 - 45/2, 270 + 45/2 ],
'NW': [ 315 - 45/2, 315 + 45/2 ]
};
function angleToCardinal(angle) {
console.log("AngleToCardinals: " + angle);
for (var k in cardinals) {
if (angle >= cardinals[k][0] && angle <= cardinals[k][1]) {
return k;
}
}
return "???";
}
var targetCoords = { latitude: 30.5625, longitude: -81.453611 };
var foundTree = false;
function gotPosition(pos) {
clearInterval(timer);
var distance = distanceBetweenCoordinates(pos.coords, targetCoords);
var angle = angleBetweenCoords(pos.coords, targetCoords);
console.log("Got Position: " + JSON.stringify(pos));
console.log("Distance: " + distance + " Angle: " + angle);
if (distance > 20) {
card.body("You are " + Math.round(distance) + "m from the goal! Direction: " + angleToCardinal(angle));
foundTree = false;
} else {
card.body("Good! You made it! Take a picture of your team with the 'Landmark Oak'\n\nThis demo was built entirely in JS - Check the source on pbl.io/jsconfscavengersource");
}
}
function positionError(err) {
clearInterval(timer);
console.log("Unable to get coordinates ... :( " + err.code + " " + err.message);
card.body("NSA does not like you ... No luck with GPS.");
}
setTimeout(function() {
navigator.geolocation.watchPosition(gotPosition, positionError, { enableHighAccuracy: true, maximumAge: 0 });
}, 3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment