Reputation: 343
How do I find closest airport using longitude and latitude ?
Any specific web services and any database to achieve ?
Upvotes: 4
Views: 14822
Reputation: 155
this.nearestAirport = this.airports.find((airport) => {
return (Math.round(airport.latitude) === Math.round(currentLocation.latitude) &&
Math.round(airport.longitude) === Math.round(currentLocation.longitude));
});
Upvotes: 0
Reputation: 1524
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
Upvotes: 8
Reputation: 3198
To Find nearest airport and to get directions to reach there from a specified point (Lat,Lan)
Here is a Google method,without any database to achieve this :
onclick="getNeighbourhood('<%= propLat %>','<%= propLan %>');"
For full code visit here FULL NEAREST AIRPORT SCRIPT AND STYLE
function getNeighbourhood(propLatQ,propLanQ) {
propLat=propLatQ;
propLan=propLanQ;
var myLatlng = new google.maps.LatLng(propLat,propLan);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
places = new google.maps.places.PlacesService(map);
google.maps.event.addListener(map, 'tilesloaded', tilesLoaded);
autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'));
google.maps.event.addListener(autocomplete, 'place_changed', function() {
showSelectedPlace();
});
Upvotes: -1
Reputation: 2693
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
}
}
Upvotes: 0
Reputation: 2307
On which platform are you coding, Durga? Is it Android?
In this case, you could use the Google Maps API:
https://developers.google.com/maps/
and, in particular, Google Places:
https://developers.google.com/places/
Broswe their documentation for details. In particular, check their license.
Upvotes: 0