Search code examples
c#gpscoordinateslatitude-longitude

Find closest city to given location


I am trying to find the closest city to given location. I have stored location of some cities that I want to work with. And I have my location, but I dont know how to find the closest city to my location ?

Cities
New York - Lat 40.714353; Long -74.005973
Washington - Lat 38.895112; Long -77.036366
....more cities

My location
Philadephia - Lat 39.952335; Long -75.163789

So how should I compare the coords to find the closest city ? I am doing program in C# but just knowing the solution of algorythm is enaught for me :) Thanks for any help


Solution

  • You should use your high school knowledge to solve this problem, your alghorithm is:

    closest = sqrt ( (lat2 - lat1) ^2 + (Long2-Long1) ^2 ) now this give you your air distance.

    so, when you do this for an array of values, you can use asort function to compare which one is closest to you.