每当我在c#中将“度分秒”转换为十进制度时,纬度和经度都会不断变化
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了每当我在c#中将“度分秒”转换为十进制度时,纬度和经度都会不断变化,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4672字,纯文字阅读大概需要7分钟。
内容图文
![每当我在c#中将“度分秒”转换为十进制度时,纬度和经度都会不断变化](/upload/InfoBanner/zyjiaocheng/672/cdf1aad2801446978605fa3e2a11ccd6.jpg)
如果输入以下位置:
纬度= 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;
}
}
如果我不对纬度或经度进行任何更改,而是单击“应用更改”(基本上再次进行上述计算),它将在数据库中生成不同的纬度和经度.每次我进行编辑但不进行更改时都会发生这种情况(我只需单击“应用更改”,它就会以不同的结果再次进行计算).
在上述情况下,新的纬度和经度是:
纬度= 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仅仅是为了使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/
在“谷歌纵横”类中查看.
内容总结
以上是互联网集市为您收集整理的每当我在c#中将“度分秒”转换为十进制度时,纬度和经度都会不断变化全部内容,希望文章能够帮你解决每当我在c#中将“度分秒”转换为十进制度时,纬度和经度都会不断变化所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。