Skip to content

Instantly share code, notes, and snippets.

@mklemme
Last active August 29, 2015 14:07
Show Gist options
  • Save mklemme/1a0526cfe02dc48bfa8b to your computer and use it in GitHub Desktop.
Save mklemme/1a0526cfe02dc48bfa8b to your computer and use it in GitHub Desktop.
// You and some friends usually play a gathering game (like a scavenger hunt) using geo-coordinates in a park, but it has gotten so popular that many people want to join in. One of your friends created an API for people to add items hidden in the park, but now you want to build an App to help people decide what to find in the park.
// You are given the following set of data as a result back from an API and you want to sort those results in terms of proximity to the user of your site.
// var myResults = [ {name: "six pack of beer", location: {lat: 37.767182, long: -122.5}},
// {name: "whacky glasses", location: {lat: 37.767182, long: -122.51}},
// {name: "whiskey bottle", location: {lat: 37.767282, long: -122.49}},
// {name: "diving goggles", location: {lat: 37.770282, long: -122.503}},
// {name: "running shoes", location: {lat: 37.7669, long: -122.457}},
// {name: "paint brushes", location: {lat: 37.76800, long: -122.4580}}]
// Write a loop that takes some results and a current location, i.e. lat and long and returns the results in order of proximity to the currentLocation. Note: you should choose this current location.
var myResults = [ {name: "six pack of beer", location: {lat: 37.767182, long: -122.5}},
{name: "whacky glasses", location: {lat: 37.767182, long: -122.51}},
{name: "whiskey bottle", location: {lat: 37.767282, long: -122.49}},
{name: "diving goggles", location: {lat: 37.770282, long: -122.503}},
{name: "running shoes", location: {lat: 37.7669, long: -122.457}},
{name: "paint brushes", location: {lat: 37.76800, long: -122.4580}}];
// pick a current location
// create a for loop that looks at all location
// in the looop: pull out all the location with lat and long
// math absolute
// create a new array with the objects of place and distance
// sort the array in distance
var currentLocation = {lat: 37.767182, long: -122.51};
console.log (currentLocation.lat);
console.log (currentLocation.long);
var distance = [];
for (var i = 0; i <= myResults.length - 1 ; i++) {
var dist = (Math.abs(currentLocation.lat - myResults[i].location.lat)) + (Math.abs(currentLocation.long - myResults[i].location.long));
distance.push({"Place": myResults[i].name , "distance" : dist});
}
distance.sort(function(a,b) {
return(a.distance - b.distance);
});
console.log (distance);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment