在计算距离并检查哪个更近后,如何访问纬度和经度?
Posted
技术标签:
【中文标题】在计算距离并检查哪个更近后,如何访问纬度和经度?【英文标题】:How can I access latitude and longitude after calculating distance and checking whis one closer? 【发布时间】:2020-12-02 11:27:42 【问题描述】:我有一个点列表,称为相交。这个列表的顺序是这样的:
latitudePoint1,longitudePoint1,latitudePoint2,longitudePoint2,latitudePoint3,longitudePoint3,...
使同一点的经纬度有不同的索引。 我计算这些点到固定点的距离,然后我需要检查其中哪一个点最接近固定点(硬编码)。 我尝试用下面的 cmets 更好地解释一切:
List<double> distanciasOcorrenciaIntersecao = new List<double>();
for (int i = 0; i <= intersect.Count(); i++)
//get latitude
double intLat = intersect[2 * i];
//get longitude
double intLong = intersect[2 * i + 1];
//calculates the distance between the fixed point and the point of the list intersect
var distancia = GetDistance(FixedLatitude, FixedLongitude, intLat.ToString(),
intLong.ToString());
//Add the latitude and longitude and long as the distance calulated to another list
distanciasOcorrenciaIntersecao.Add(intLat);
distanciasOcorrenciaIntersecao.Add(intLong);
distanciasOcorrenciaIntersecao.Add(distancia);
现在我需要检查添加到列表 distanciasOcorrenciaIntersecao 的距离中哪一个距离固定点更近,然后返回与该距离相关的纬度和经度。
这是我到目前为止所做的:
for (int j = 0; j <= distanciasOcorrenciaIntersecao.Count(); j++)
// I need to at least have two point in the intersect list,
// meaning 4 elements, latitude1,longitude1,latitude2,longitude2
if (intersect.Count() >= 4)
if (distanciasOcorrenciaIntersecao[3 * j + 2]
< distanciasOcorrenciaIntersecao[3 * j + 5])
//....?
我不确定这是否是解决此问题的最佳方法,但如果有人知道更好的方法,请提供帮助。谢谢
【问题讨论】:
如果不是拥有double
值的列表,而是拥有一个包含纬度、经度和距离值的类列表,这可能会更容易?
您的 for 循环需要增加 2 而不是 1。所以从 : i++ 改为 : i += 2
我可以这样做,但如果它不在课堂上对我来说会更有意义
为什么GetDistance
将intLat
和intLong
作为字符串?
【参考方案1】:
List<double> distanciasOcorrenciaIntersecao = new List<double>();
for (int i = 0; i <= intersect.Count(); i++)
double intLat = intersect[2 * i];
double intLong = intersect[2 * i + 1];
var distancia = GetDistance(ocorrencia.Latitude, ocorrencia.Longitude, intLat.ToString(), intLong.ToString());
distanciasOcorrenciaIntersecao.Add(intLat);
distanciasOcorrenciaIntersecao.Add(intLong);
distanciasOcorrenciaIntersecao.Add(distancia);
double minimum = 0;
int index = 0;
int j = 2;
for ( j = 2; j <= distanciasOcorrenciaIntersecao.Count()-3; j+=3)
if (intersect.Count() >= 4)
if (j == 2)
if (distanciasOcorrenciaIntersecao[j] < distanciasOcorrenciaIntersecao[j + 3])
minimum = distanciasOcorrenciaIntersecao[j];
index = j;
else
minimum = distanciasOcorrenciaIntersecao[j + 3];
index = j + 3;
else
if (minimum > distanciasOcorrenciaIntersecao[j + 3])
minimum = distanciasOcorrenciaIntersecao[j + 3];
index = j + 3;
【讨论】:
【参考方案2】:您可以使用ValueTuple
和 LINQ 来简化这个过程,而不是使用一个类:
var distanciasOcorrenciaIntersecao = new List<(double lat, double lon, double dist)>();
if (intersect.Count() > 2)
for (int i = 0; i < intersect.Count(); i += 2)
//get latitude
double intLat = intersect[i];
//get longitude
double intLong = intersect[i + 1];
//calculates the distance between the fixed point and the point of the list intersect
var distancia = GetDistance(FixedLatitude, FixedLongitude, intLat.ToString(), intLong.ToString());
//Add the latitude and longitude and long as the distance calculated to another list
distanciasOcorrenciaIntersecao.Add((intLat, intLong, distancia));
var (closestLat, closestLon, closestDist) = distanciasOcorrenciaIntersecao.OrderBy(doi => doi.dist).First();
【讨论】:
以上是关于在计算距离并检查哪个更近后,如何访问纬度和经度?的主要内容,如果未能解决你的问题,请参考以下文章