gpt4 book ai didi

c# - 每次我在 c# 中从度分秒转换为十进制度时,纬度和经度都会不断变化

转载 作者:行者123 更新时间:2023-11-30 15:46:22 25 4
gpt4 key购买 nike

如果我输入位置:

纬度 = 28 度 45 分 12 秒经度 = 81 度 39 分 32.4 秒

使用以下代码将其转换为十进制格式以存储在数据库中:

Coordinates coordinates = new Coordinates();
coordinates.LatitudeDirection = this.radLatNorth.Checked ? Coordinates.Direction.North : Coordinates.Direction.South;
coordinates.LatitudeDegree = this.ConvertDouble(this.txtLatDegree.Text);
coordinates.LatitudeMinute = this.ConvertDouble(this.txtLatMinute.Text);
coordinates.LatitudeSecond = this.ConvertDouble(this.txtLatSecond.Text);
coordinates.LongitudeDirection = radLongEast.Checked ? Coordinates.Direction.East : Coordinates.Direction.West;
coordinates.LongitudeDegree = this.ConvertDouble(this.txtLongDegree.Text);
coordinates.LongitudeMinute = this.ConvertDouble(this.txtLongMinute.Text);
coordinates.LongitudeSecond = this.ConvertDouble(this.txtLongSecond.Text);
//gets the calulated fields of Lat and Long
coordinates.ConvertDegreesMinutesSeconds();

在上面的代码中,ConvertDouble 定义为:

  private double ConvertDouble(string value)
{
double newValue = 0;
double.TryParse(value, out newValue);

return newValue;
}

ConvertDegreesMinutesSeconds 定义为:

public void ConvertDegreesMinutesSeconds()
{
this.Latitude = this.LatitudeDegree + (this.LatitudeMinute / 60) + (this.LatitudeSecond / 3600);
this.Longitude = this.LongitudeDegree + (this.LongitudeMinute / 60) + (this.LongitudeSecond / 3600);

//adds the negative sign
if (LatitudeDirection == Direction.South)
{
this.Latitude = 0 - this.Latitude;

}
else if (LongitudeDirection == Direction.West)
{
this.Longitude = 0 - this.Longitude;
}
}

如果我不对纬度或经度做任何更改,然后单击 Apply Changes,这基本上会再次执行上述计算,它会在数据库中生成不同的纬度和经度。每次我去编辑它但不进行更改时都会发生这种情况(我只需单击“应用更改”,它会再次计算并得到不同的结果)。

在上面的场景中,新的纬度和经度是:

纬度 = 28 度 45 分 12 秒经度 = 81 度 40 分 32.4 秒

如果我再做一次,它会变成:

纬度 = 28 度 45 分 12 秒经度 = 81 度 41 分 32.4 秒

另一部分是,当我进入编辑状态时,它采用经纬度的十进制格式,并将其转换为度分秒格式,并将它们放入各自的文本框中。代码是:

public void SetFields()
{
Coordinates coordinateLocation = new Coordinates();
coordinateLocation.Latitude = this.Latitude;
coordinateLocation.Longitude = this.Longitude;
coordinateLocation.ConvertDecimal();

this.radLatNorth.Checked =
coordinateLocation.LatitudeDirection == Coordinates.Direction.North;
this.radLatSouth.Checked = !this.radLatNorth.Checked;
this.txtLatDegree.Text = coordinateLocation.LatitudeDegree.ToString().Replace("-", string.Empty);
this.txtLatMinute.Text = Math.Round(coordinateLocation.LatitudeMinute, 0).ToString().Replace("-", string.Empty);
this.txtLatSecond.Text = Math.Round(coordinateLocation.LatitudeSecond, 2).ToString().Replace("-", string.Empty);

this.radLongEast.Checked =
coordinateLocation.LongitudeDirection == Coordinates.Direction.East;
this.radLongWest.Checked = !this.radLongEast.Checked;
this.txtLongDegree.Text = coordinateLocation.LongitudeDegree.ToString().Replace("-", string.Empty); ;
this.txtLongMinute.Text = Math.Round(coordinateLocation.LongitudeMinute, 0).ToString().Replace("-", string.Empty);
this.txtLongSecond.Text = Math.Round(coordinateLocation.LongitudeSecond, 2).ToString().Replace("-", string.Empty);
}

从上面的例子可以看出,Minute一直在增加1,这说明为什么它在数据库中生成了不同的十进制经纬度,所以我猜问题更多在上面,但我不确定它在哪里或为什么这样做?

  public void ConvertDecimal()
{
this.LatitudeDirection = this.Latitude > 0 ? Direction.North : Direction.South;
this.LatitudeDegree = (int)Math.Truncate(this.Latitude);
if (LatitudeDirection == Direction.South)
{
this.LatitudeDegree = 0 - this.LatitudeDegree;
}
this.LatitudeMinute = (this.Latitude - Math.Truncate(this.Latitude)) * 60;
this.LatitudeSecond = (this.LatitudeMinute - Math.Truncate(this.LatitudeMinute)) * 60;

this.LongitudeDirection = this.Longitude > 0 ? Direction.East : Direction.West;
this.LongitudeDegree = (int)Math.Truncate(this.Longitude);
if (LongitudeDirection == Direction.West)
{
this.LongitudeDegree = 0 - this.LongitudeDegree;
}
this.LongitudeMinute = (this.Longitude - Math.Truncate(this.Longitude)) * 60;
this.LongitudeSecond = (this.LongitudeMinute - Math.Truncate(this.LongitudeMinute)) * 60;
}

最佳答案

您需要截断您的分钟(也许还有秒,我不确定)而不是四舍五入。请注意,下面使用的 Math.Round 只是为了使分/秒分量达到所需的有效数字位数。

double Minutes(double decimalDegrees)
{
//Get hours
double h = Math.Truncate(lat);
//Get minutes + seconds
double x = (Math.Abs(h - Math.Round(lat, 12)) * 60.0);
//Everything after the decimal is seconds
double min = Math.Truncate(x);
return min;
}

我将此转换代码基于此 codeplex 项目:http://geoframework.codeplex.com/查看 Latitude 类。

关于c# - 每次我在 c# 中从度分秒转换为十进制度时,纬度和经度都会不断变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4378984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com