Find closest airport based on latitude and longitude
Solution 1
- You need a dataset with fields for the airport`s latitude and longitude
- Use the calculation for Great-Circle distance (GCD) as outlined on the page linked below
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)
From both sites you can download a "static" airports list, to perform offline search.
Regards
Related videos on Youtube
Durga Prasad
Updated on September 15, 2022Comments
-
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 over 11 yearsI'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 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 over 11 yearsMight 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 over 11 yearsDo 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 over 11 yearsYes, I have airports database along with latitude and longitude and I have user current location (latitude and longitude).
-
AlexBottoni over 11 yearsIn 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 over 11 yearsI 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?