Guest User

Untitled

a guest
Nov 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var userLat = 33.000;
  2. var userLng = 44.000;
  3.  
  4. var garages = [
  5.     {
  6.         name: "garage 1",
  7.         lat: 33.111,
  8.         lng: 44.111,
  9.         distance: -1
  10.     },
  11.     {
  12.         name: "garage 2",
  13.         lat: 33.222,
  14.         lng: 44.222,
  15.         distance: -1
  16.     }
  17. ];
  18.  
  19.  
  20. function calcDistance(lat1, lng1, lat2, lng2) {
  21.     // hamsandwich ...
  22. }
  23.  
  24. function populateDistances() {
  25.     for (var i = 0; i < objects.length; i++) {
  26.         //loop through each garage object.  set the "distance" property based on the user location.
  27.         garages[i].distance = calcDistance(userLat, userLng, garages[i].lat, garages[i].lng);
  28.     }
  29. }
  30.  
  31. function findClosestGarageIndex() {
  32.     var closestIndex = -1;
  33.     var closestDistance = -1;
  34.     for (var i = 0; i < objects.length; i++) {
  35.         //for each of the garage objects ...
  36.         if (closestIndex == -1 || (garages[i].distance < closestDistance)) {
  37.             //if this is the first garage we're looping through (closestIndex == -1) ....
  38.             //or if this garage is closer than any of the other garages ...
  39.             //mark this garage as the closest.
  40.             closestIndex = i;
  41.             closestDistance = garages[i].distance;
  42.         }
  43.     }
  44.     //we will return the index of the closest garage.
  45.     return i;
  46. }
  47.  
  48.  
  49. init() {
  50.     populateDistances();
  51.     var closestGarageIndex = findClosestGarageIndex();
  52.     console.log("The closest garage is " + garages[closestGarageIndex].name);
  53. }
Add Comment
Please, Sign In to add comment