Find closest airport based on latitude and longitude

13,319

Solution 1

  1. You need a dataset with fields for the airport`s latitude and longitude
  2. Use the calculation for Great-Circle distance (GCD) as outlined on the page linked below

Wikipedia article on GCD

Please provide example code/specify the language if you would like further and more specific help

CODE:

Taken from another webpage (now defunct, used 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;  
        }  
    }  
}  

Pseudocode:

This pseudocode should give you the answer you are looking for. I didn't test this and the C# will probably have syntactic errors but the gist of it should be clear.

/* Set parameters */
Position currentPosition = new Position();
Position airportPosition = new Position();
Double minDistance = Double.MaxValue;
String closestAirportName = "UNKNOWN";
Haversine hv = new Haversine();

/* Set current position, remains fixed throughout */
currentPosition.Latitude = 0.000;
currentPosition.Longitude = 0.000; 

/* Compare distance to each airport with current location
* and save results if this is the closest airport so far*/
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
    }
}

Solution 2

One WebService I found is airports.pidgets.com

This is an example:

XML format http://airports.pidgets.com/v1/airports?near=45.3515,9.3753

JSon format http://airports.pidgets.com/v1/airports?near=45.3515,9.3753&format=json

[Edit] Found another webservice on aviationweather.gov (only XML and CSV)

http://aviationweather.gov/adds/dataserver_current/httpparam?dataSource=stations&requestType=retrieve&format=xml&radialDistance=20;9.3753,45.3515

From both sites you can download a "static" airports list, to perform offline search.

Regards

Share:
13,319

Related videos on Youtube

Durga Prasad
Author by

Durga Prasad

Updated on September 15, 2022

Comments

  • Durga Prasad
    Durga Prasad over 1 year

    How do I find closest airport using longitude and latitude ?

    Any specific web services and any database to achieve ?

    • David
      David over 11 years
      I'm afraid this is a bit off-topic for Stack Overflow. We can certainly help you with the code when you're trying to integrate to such a service, but recommendations for services aren't generally objectively answerable.
  • David
    David over 11 years
    @user1273278: If you need a specific implementation of a distance algorithm along the curved surface of the Earth, I have an old blog post that does it in SQL: publicvoidlife.blogspot.com/2011/02/… Translating it into C# should be pretty simple, it's mostly just parentheses, basic arithmetic, and built-in math functions.
  • Durga Prasad
    Durga Prasad over 11 years
    Might be, I didn't write my question very clearly. When user open my application I can find Latitude and Longitude, using those parameters I need to find out nearest airport.
  • JustinJDavies
    JustinJDavies over 11 years
    Do you have the data for Lat/Lon of the airports that you would like to consider? If you do then just calculate the distance from your current position to each airport in turn, using the Haversine function above, and save a reference to any airport that gives you a shorter distance than any previous airport
  • Durga Prasad
    Durga Prasad over 11 years
    Yes, I have airports database along with latitude and longitude and I have user current location (latitude and longitude).
  • AlexBottoni
    AlexBottoni over 11 years
    In this case, you can use Google Maps in your web pages and you can access Google Maps/Places API from Javascript. See: developers.google.com/maps, developers.google.com/maps/documentation/javascript and developers.google.com/maps/documentation/javascript/tutorial
  • JustinJDavies
    JustinJDavies over 11 years
    I added pseudocode to illustrate the algorithm you need. Could you paste your own code example into the Question so that a specific answer can be supplied?