根据下面的代码,如何将对象列表的值与测试值进行比较?
我正在构建地理定位应用程序。我将在经度和纬度上通过,并希望服务回答最接近这些值的位置。
我开始转换为字符串的路径,并将值格式化为两个小数位,但这似乎有点太多了,我正在寻找更优雅的解决方案。
public class Location : IEnumerable
{
public string label { get; set; }
public double lat { get; set; }
public double lon { get; set; }
//Implement IEnumerable
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
}
[HandleError]
public class HomeController : Controller
{
private List<Location> myList = new List<Location>
{
new Location {
label="Atlanta Midtown",
lon=33.657674,
lat=-84.423130},
new Location {
label="Atlanta Airport",
lon=33.794151,
lat=-84.387228},
new Location {
label="Stamford, CT",
lon=41.053758,
lat=-73.530979}, ...
}
public static int Main(String[] args)
{
string inLat = "-80.987654";
double dblInLat = double.Parse(inLat);
// here's where I would like to find the closest location to the inLat
// once I figure out this, I'll implement the Longitude, and I'll be set
}
答案 0 :(得分:3)
如果你不想得到奇怪的结果,你会想要使用正确的距离公式:
double CalculateDistance(double lat1, double lon1, double lat2, double lon2)
{
const double R = 6371;
return Math.Acos(
Math.Sin(lat1) * Math.Sin(lat2) +
Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(lon2 - lon1)) * R;
}
我希望这是正确的公式,我的数学可能会有点生疏。所有参数都需要以rad为单位,所以如果你以度为单位输入输入,那么也要写一个实用方法:
double DegToRad(double deg)
{
return deg * Math.PI / 180.0;
}
无论如何,在那之后,你可以找出最短的距离:
Location GetClosestLocation(Location origin)
{
double olatr = DegToRad(origin.Lat);
double olonr = DegToRad(origin.Lon);
return
(from l in locations
let latr = DegToRad(l.Lat)
let lonr = DegToRad(l.Lon)
orderby CalculateDistance(latr, lonr, olatr, olonr))
.FirstOrDefault();
}
这在技术上不是最高性能的解决方案,因为它必须进行排序,但是没有漂亮的Linq扩展方法来做投影min。如果你想要,你必须编写自己的foreach
循环:
Location GetClosestLocation(Location origin)
{
double olatr = DegToRad(origin.Lat);
double olonr = DegToRad(origin.Lon);
Location closest = null;
double minDistance = double.MaxValue;
foreach (Location l in locations)
{
double latr = DegToRad(l.Lat);
double lonr = DegToRad(l.Lon);
double dist = CalculateDistance(latr, lonr, olatr, olonr));
if (dist < minDistance)
{
minDistance = dist;
closest = l;
}
}
return closest;
}
答案 1 :(得分:0)
我认为最简单的方法是做到以下几点。但不是大多数表现者:)
遍历列表并计算每个位置与参考位置之间的距离。在每一步,检查这是否是你到目前为止看到的最短距离并存储。一旦到达列表的末尾,您将在存储的变量中拥有最接近的位置。
如果您正在谈论大量的地点并且计划进行许多这种性质的空间查询,您可以考虑在数据上设置四叉树索引。
这是我在快速'Bing'后找到的链接,它应该有助于距离计算,我希望。请参阅此链接:
http://www.delphiforfun.org/Programs/Math_Topics/Lat-Long%20Distance.htm
答案 2 :(得分:0)
您需要多准确?它被称为大圆距离。
答案 3 :(得分:0)
我发现有人创建this,使用几种不同的方法之一计算全球两个距离之间的距离。我不得不将.NET项目转换为更新的VS2008,但这似乎工作正常。然后我将这个项目添加到我的解决方案中并引用它。
我的代码变成了:
string inLat = "-80.987654";
string inLon = "33.521478";
var miles = GetNearestLocation(inLat, inLon);
public double GetNearestLocation(string lat, string lon)
{
double dblInLat = double.Parse(lat);
double dblInLon = double.Parse(lon);
// instantiate the calculator
GeodeticCalculator geoCalc = new GeodeticCalculator();
// select a reference elllipsoid
Ellipsoid reference = Ellipsoid.WGS84;
// set user's current coordinates
GlobalCoordinates userLocation;
userLocation = new GlobalCoordinates(
new Angle(dblInLon), new Angle(dblInLat)
);
// set example coordinates- when fully fleshed out,
// this would be passed into this method
GlobalCoordinates testLocation;
testLocation= new GlobalCoordinates(
new Angle(41.88253), new Angle(-87.624207) // lon, then lat
);
// calculate the geodetic curve
GeodeticCurve geoCurve = geoCalc.CalculateGeodeticCurve(reference, userLocation, testLocation);
double ellipseKilometers = geoCurve.EllipsoidalDistance / 1000.0;
double ellipseMiles = ellipseKilometers * 0.621371192;
/*
Console.WriteLine("2-D path from input location to test location using WGS84");
Console.WriteLine(" Ellipsoidal Distance: {0:0.00} kilometers ({1:0.00} miles)", ellipseKilometers, ellipseMiles);
Console.WriteLine(" Azimuth: {0:0.00} degrees", geoCurve.Azimuth.Degrees);
Console.WriteLine(" Reverse Azimuth: {0:0.00} degrees", geoCurve.ReverseAzimuth.Degrees);
*/
return ellipseMiles;
}