【发布时间】:2010-12-07 16:27:13
【问题描述】:
如果我输入的位置:
纬度 = 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;
}
【问题讨论】:
-
你能发布 CoordinateLocation.ConvertDecimal 代码吗?
-
ConvertDecimal 代码已发布。