找到最接近的匹配双打的阵列 [英] find nearest match to array of doubles
问题描述
由于code以下,我该如何比较对象的值列表与测试值?
我建立一个应用程序地理位置。我将传递的经度和纬度,并希望有服务答案回来最接近这些值的位置。
我开始向下转换成字符串,并格式化值下降到小数点后两位的路径,但似乎有点过于贫民窟,我正在寻找一个更好的解决方案。
公共类地点:IEnumerable的
{
公共字符串标签{搞定;组; }
公共双纬度{搞定;组; }
公共双LON {搞定;组; } //实现IEnumerable
公众的IEnumerator的GetEnumerator()
{
返回(IEnumerator的)这一点;
}}
[的HandleError]
公共类HomeController的:控制器
{
私人列表<地点> myList中=新名单<地点>
{
新的位置{
标签=亚特兰大市中心
LON = 33.657674,
纬度= -84.423130},
新的位置{
标签=亚特兰大机场
LON = 33.794151,
纬度= -84.387228},
新的位置{
标签=康涅狄格州斯坦福德
LON = 41.053758,
纬度= -73.530979},...
} 公共静态INT主要(字串[] args)
{
字符串inLat =-80.987654;
双dblInLat = double.Parse(inLat); //这里的地方,我想找到最近的位置到inLat
//一旦我弄清楚这一点,我将实现经度,我会被设置
}
我发现的这个其中有人创建了跨计算使用几种不同的方法之一,全球两个距离之间的距离。我必须在.NET项目转换为VS2008更新,但似乎工作的罚款。然后,我只是说这个项目我的解决方案,并提出了参考吧。
我的code,则变成了:
字符串inLat =-80.987654;
字符串inLon =33.521478;
VAR英里= GetNearestLocation(inLat,inLon);公共双GetNearestLocation(字符串纬度,字符串LON)
{
双dblInLat = double.Parse(LAT);
双dblInLon = double.Parse(LON); //实例计算器
GeodeticCalculator geoCalc =新GeodeticCalculator(); //选择参考elllipsoid
参考椭球= Ellipsoid.WGS84; //设置用户的当前坐标
GlobalCoordinates userLocation;
userLocation =新GlobalCoordinates(
新角(dblInLon),新视角(dblInLat)
); //设置例如coordinates-时完全充实,
//这会被传入此方法
GlobalCoordinates testLocation;
testLocation =新GlobalCoordinates(
新角(41.88253),新视角(-87.624207)//经度,纬度,然后
); //计算大地曲线
GeodeticCurve geoCurve = geoCalc.CalculateGeodeticCurve(参考,userLocation,testLocation);
双ellipseKilometers = geoCurve.EllipsoidalDistance / 1000.0;
双ellipseMiles = ellipseKilometers * 0.621371192;
/ *
Console.WriteLine(从输入位置的2-D路径中使用WGS84测试位置);
Console.WriteLine(椭球距离:{0:0.00}公里({1:0.00}英里),ellipseKilometers,ellipseMiles);
Console.WriteLine(方位角:{0:0.00}度,geoCurve.Azimuth.Degrees);
Console.WriteLine(反向方位角:{0:0.00}度,geoCurve.ReverseAzimuth.Degrees);
* /
返回ellipseMiles;
}
Given the code below, how do I compare a List of objects's values with a test value?
I'm building a geolocation application. I'll be passing in longitude and latitude and would like to have the service answer back with the location closest to those values.
I started down the path of converting to a string, and formatting the values down to two decimal places, but that seemed a bit too ghetto, and I'm looking for a more elegant solution.
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
}
I found this which someone created that calculates distances between two distances across the globe using one of several different methods. I had to convert the .NET project to the updated VS2008, but that seemed to work fine. Then I just added this project to my solution and made a reference to it.
My code then became:
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;
}
这篇关于找到最接近的匹配双打的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!