如果我输入的位置:
纬度= 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(基本上再次执行上述calucation),它会在数据库中生成不同的纬度和经度。每次我编辑它并且不进行更改时都会发生这种情况(我只需单击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);
}
从上面的例子中,你可以看到分钟继续增加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;
}
答案 0 :(得分:3)
你需要截断你的分钟(也许是秒,我不确定)而不是圆。请注意,下面使用的Math.Round只是为了使Minutes / Seconds组件达到所需的有效位数。
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课程。
答案 1 :(得分:1)
在显示它们时,你会将你的分钟和秒数四舍五入,而不是截断它们或使用Math.Floor。由于你有超过30秒的时间,你的分钟的分数值超过0.5,所以它会向上舍入。