根据纬度和经度查找最近的机场

4

如何使用经纬度来查找最近的机场?

是否有特定的网络服务或数据库可供使用?


2
很抱歉,这个问题与 Stack Overflow 的主题有些偏离。当您尝试集成此类服务时,我们可以帮助您编写代码,但是对于服务的推荐通常没有客观可答复性。 - David
你能否发布一下你目前的代码示例? - JustinJDavies
5个回答

8

我发现的一个Web服务是airports.pidgets.com

这是一个例子:

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

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

[编辑] 在aviationweather.gov上找到另一个Web服务(仅限XML和CSV)

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

你可以从这两个网站下载“静态”机场列表,以进行离线搜索。

祝好


0
  1. 您需要一个包含机场纬度和经度字段的数据集
  2. 使用以下链接页面中概述的大圆距离(GCD)计算方法

GCD维基百科文章

如果您需要更具体的帮助,请提供示例代码/指定语言

代码:

从另一个网页中获取(现已失效,使用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;  
        }  
    }  
}  

伪代码:

这个伪代码应该能给你想要的答案。我没有测试过,C# 可能会有语法错误,但它的主旨应该是清楚的。

/* 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
    }
}

@user1273278:如果您需要在曲面地球上实现特定的距离算法,我有一篇旧的博客文章使用SQL实现了它:http://publicvoidlife.blogspot.com/2011/02/shortest-distance-between-two-points-is.html。将其翻译成C#应该很简单,主要是括号、基本算术和内置数学函数。 - David
也许我没有很清楚地表达我的问题。当用户打开我的应用程序时,我可以找到纬度和经度,使用这些参数,我需要找到最近的机场。 - Durga Prasad
你是否有所需考虑的机场的纬度/经度数据?如果有,只需使用上述Haversine函数依次计算当前位置到每个机场的距离,并保存对于任何先前机场给出更短距离的参考。 - JustinJDavies
是的,我有机场数据库,其中包含纬度和经度信息,并且我还有用户当前位置(纬度和经度)。 - Durga Prasad
我添加了伪代码来说明你需要的算法。你能把自己的代码示例粘贴到问题中,以便提供具体的答案吗? - JustinJDavies

0

在这种情况下,您可以在网页中使用Google Maps,并且可以从Javascript访问Google Maps/Places API。请参见:https://developers.google.com/maps/,https://developers.google.com/maps/documentation/javascript/和https://developers.google.com/maps/documentation/javascript/tutorial - AlexBottoni

0
this.nearestAirport = this.airports.find((airport) => {
              return (Math.round(airport.latitude) === Math.round(currentLocation.latitude) &&
                      Math.round(airport.longitude) === Math.round(currentLocation.longitude));
            });

-1

查找最近的机场并获取从指定点(Lat,Lan)到达那里的方向

这里有一种谷歌方法,不需要任何数据库就可以实现:

onclick="getNeighbourhood('<%= propLat %>','<%= propLan %>');"

完整代码请访问此处 完整最近机场脚本和样式

Find nearest airport

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();
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接