1

I am trying to work out the shortest distance between the user location and from some collection of coordinates received from an API, and then I used Leaflet Routing Machine to workout the routes. I'm not sure if I am doing it correctly because when I changed the user location, I still get the same route. Could some one please have a look at my code and advice if I am doing the right thing? I have the code in Jfiddle.

MY APPROACH:

1) When I have received the latlng for user location and from an array of coordinate from an API. I use the Great-circle distance formula Jfiddle to find the distances between all the points and push them in an array called allDistances

2)After that I look for the shortest distance in the array by using this formula

function closestDistance() {
 return allDistances.reduce((acc, loc) =>
  acc.distance < loc.distance ? acc : loc
 );
}

This is my CODE in http://jsfiddle.net/jt100/r740b3d2/47/ , please kindly note I have hard coded the user location because all the points are located in the US and my current location is in UK so I can't identify routes between countries.

I am still very new to programming, It would also help me a lot if you could please give me some advice on how I could improve my code. I have been reading about SOLID principle. I want to follow this rule but I’m not sure what I need to change in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.