0

On one hand I have a main latitude and a longitude 40.4168,-3.7038,for example (both separated like this: {latitude:40.4168 , longitude:-3.7038}) and on the other hand I have an array of places that have latitude and longitude data (both separated too like this

[
    {lat1:lat1Value,lon1:lon1Value},
    {lat2:lat2Value,lon2:lon2Value},
    {lat3:lat3Value,lon3:lon3Value}
...]

I want to calculate the nearest location of this array to my main latitude-longitude, what is the most efficient/cleanest way to resolve this problem in javascript ??

Monasha
  • 711
  • 2
  • 16
  • 27
Charly
  • 166
  • 14
  • How large can this array of places be? Typical length? – kumardeepakr3 Nov 16 '16 at 12:10
  • 2
    http://stackoverflow.com/questions/21279559/geolocation-closest-locationlat-long-from-my-position – Mahi Nov 16 '16 at 12:13
  • @Mahi The code still loops over all entries every time. O(n) complexity. Only benefit is that it is taking earth's curvature into account. If the length of array is large, then this is not a good way. – kumardeepakr3 Nov 16 '16 at 12:18
  • @DeepakKumar there can't be a better solution as you have to take all values from the array into account – Bergi Nov 16 '16 at 12:21
  • @Bergi completely agreed if the array is used only once. But if one needs to operate on the same array multiple times (scenario like nearest ATMs in a given location), then one can create a data structure like QuadTree to reduce the search domain for next query. Anyways if the length of the array is small then one can go with Mahi's code. A downside of QuadTree is construction will take some time. Also it doesn't take earth's curvature into account (creating for 2D space). – kumardeepakr3 Nov 16 '16 at 12:32
  • 2
    @DeepakKumar Yeah, but given the OP didn't tell us anything about his use case, don't read more into it than necessary :-) – Bergi Nov 16 '16 at 12:35
  • @Bergi Agreed :-) , no use case mentioned. – kumardeepakr3 Nov 16 '16 at 12:40
  • @DeepakKumar the use case is the following: I have the latitude and longitude of a building and I want the nearest specified company building to this building ( I suppose I have all the latitudes-longitudes of this buildings ) – Charly Nov 16 '16 at 12:52
  • 2
    What exactly is your issue? Calculating the distance between two coordinates? Or figuring out which distance is the shortest? – deceze Nov 16 '16 at 13:00
  • Calculate the nearest distance between a main latitude-longitude and an array of latitudes-longitudes obtaining the nearest one of this array to my main latitude-longitude – Charly Nov 16 '16 at 13:16
  • Yeah, we got that. There's two parts to that: calculate the distance between any two coordinates, and then as a secondary step figure out which is the shortest. 1) Use the Great Circle Distance Formula to calculate the distance between two coordinates. 2) Apply that either in a loop to find the smallest value (same as if you'd try to find the smallest number in an array), or an array sort to sort all coordinates by distance, … Does that help at all…? – deceze Nov 16 '16 at 13:28
  • @deceze could you provide a little example ??? – Charly Nov 16 '16 at 14:38

0 Answers0