- You need a dataset with fields for airport latitude and longitude
- Use the calculation for the Grand Circle distance (GCD) as indicated on the page below.
Wikipedia article on GCD
Please provide an example code / indicate the language if you want additional and more specific help.
CODE:
Taken from another webpage (currently non-existent, using waybackmachine )
using System; namespace HaversineFormula { /// <summary> /// The distance type to return the results in. /// </summary> public enum DistanceType { Miles, Kilometers }; /// <summary> /// Specifies a Latitude / Longitude point. /// </summary> public struct Position { public double Latitude; public double Longitude; } class Haversine { /// <summary> /// Returns the distance in miles or kilometers of any two /// latitude / longitude points. /// </summary> /// <param name="pos1β³></param> /// <param name="pos2β³></param> /// <param name="type"></param> /// <returns></returns> public double Distance(Position pos1, Position pos2, DistanceType type) { double R = (type == DistanceType.Miles) ? 3960 : 6371; double dLat = this.toRadian(pos2.Latitude - pos1.Latitude); double dLon = this.toRadian(pos2.Longitude - pos1.Longitude); double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(this.toRadian(pos1.Latitude)) * Math.Cos(this.toRadian(pos2.Latitude)) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2); double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a))); double d = R * c; return d; } /// <summary> /// Convert to Radians. /// </summary> /// <param name="val"></param> /// <returns></returns> private double toRadian(double val) { return (Math.PI / 180) * val; } } }
pseudo code:
This pseudo code should give you the answer you are looking for. I have not tested this, and C # will probably have syntax errors, but the gist of this should be clear.
Position currentPosition = new Position(); Position airportPosition = new Position(); Double minDistance = Double.MaxValue; String closestAirportName = "UNKNOWN"; Haversine hv = new Haversine(); currentPosition.Latitude = 0.000; currentPosition.Longitude = 0.000; Foreach (airport in airports) { airportPosition = new Position(airport.Lat, airport.Lon); Double distanceToAirport = hv.Distance(currentPosition, airportPosition, DistanceType.Kilometers) if (distanceToAirport < minDistance) { minDistance = distanceToAirport closestAirportName = airport.Name } }
source share