0

I am trying to obtain a users location i.e,. lat long values and compare it with a set of coordinates that I have in a CSV file.

My csv file looks like this

 Lat      Long     NAME
36.413  -97.698     p
36.146  -97.289     q
36.348  -97.157     r
36.106  -97.082     s

If the users location is 36.412954 , -97.598884 , is there a way to obtain the NAME by comparing the lat long values in the CSV file even though they are not exactly same as the users location?

It should return the coordinate pair with Name 'p'

sumit
  • 15,003
  • 12
  • 69
  • 110
joe
  • 13
  • 2

2 Answers2

0

You can just compute the difference between the user input and the CSV values. Then see if the difference is below a specified threshold. Something like:

    var err = Math.abs( a - b );

    if( err < threshold ) {
        // logic
    }
user2731223
  • 114
  • 8
0

Since we are just trying to find the nearest approximation so I will simply use two point canvas formula which will be the fastest approach here.

let positions = [
  {latitude: 36.413, longitude: -97.698, name: "p"},
  {latitude: 36.146, longitude: -97.289, name: "q"},
  {latitude: 36.348, longitude: -97.157, name: "r"},
  {latitude: 36.106, longitude: -97.082, name: "s"},
];

let x=36.412954;
let y= -97.59888;
let tolerance=0.1;

let positions_close=positions.filter(p=>Math.hypot(p.latitude-x, p.longitude-y)<tolerance);

console.log(positions_close);
sumit
  • 15,003
  • 12
  • 69
  • 110