0

I'm trying to show the nearest branch location in a map.SO i want to get the branch which has the least difference between the current location and the branch location. i have taken the differenec like this.

for(var i=0;i<$scope.locations.BranchAndAtms.length;i++){
                var mapObject = $scope.locations.BranchAndAtms[i];

                var differenceLat = (mapObject.lat - c.latitude);
                var differenceLon = (mapObject.lon - c.longitude);

                var Difference = (differenceLat + differenceLon);
                alert(Difference );

Now i get the difference between every branch in my array.how to take the value with the least difference.

NOTE: difference i get comes in minus as well as positive values.

CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182

2 Answers2

0

Outside of your for loop, you'll want to declare a variable that is going to keep track of which array index has the lowest value and another for what the lowest value currently is. By default, it will store the very first item as the lowest - this comes in handy if there is only one item in your array.

var lowest_index = 0;
var lowest_value = 0;

Inside of your for loop, you'll want to take the absolute values of the differences.

for... 
var differenceLat = Math.abs(mapObject.lat - c.latitude);
var differenceLon = Math.abs(mapObject.lon - c .longitude);

Now, compare the existing lowest value to the current one, and if the current is lower, set lowest to that value of i.

if(Difference < lowest_value) {
  lowest_index = i;
  lowest_value = Difference;
}

So, your code should look like:

var lowest_index = 0;
var lowest_value = 0;

for(var i=0;i<$scope.locations.BranchAndAtms.length;i++){
                var mapObject = $scope.locations.BranchAndAtms[i];

                var differenceLat = Math.abs(mapObject.lat - c.latitude);
                var differenceLon = Math.abs(mapObject.lon - c.longitude);

                var Difference = (differenceLat + differenceLon);

                if(Difference < lowest_value) {
                  lowest_index = i;
                  lowest_value = Difference;
                }
}
jmustonen
  • 470
  • 3
  • 8
SteveK
  • 995
  • 5
  • 17
0

In addition to Steve's answer, I'd recommend taking a look at how to correctly calculate distances between locations: Calculate distance between two latitude-longitude points? (Haversine formula)

So, create a function for calculating the distance:

function calculateDistance(currentLocation, branchLocation) {
  /* the actual implementation, use answers from
   * https://stackoverflow.com/questions/27928/ as reference 
   *
   * Note that if you want, you could also use the actual latitude 
   * and longitude values as parameters for this function:
   * calculateDistance(currentLat, currentLon, branchLat, branchLon)
   */
  return distance;
}

And then use it inside the for-loop instead of the current difference calculations:

var Difference = calculateDistance(c, mapObject);
// or if you want to have the separate latitude and longitude parameters:
// var Difference = calculateDistance(c.latitude, c.longitude, mapObject.lat, mapObject.lon);

Otherwise Steve's approach is valid to follow.

(If you'd like to show for example all branch locations within given distance, or all the nearest branch locations if there are more than one branch with exactly the same distance, you'll need to store the distances and sort by them. But this goes out of the topic, as you did want to show only one branch location.)

Community
  • 1
  • 1
jmustonen
  • 470
  • 3
  • 8