[英]Latitude and Longitude keep changing every time I convert from Degrees Minutes Seconds to Decimal Degrees in c#
如果我進入以下位置:
緯度= 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;
}
你需要截斷你的會議紀要(也許是秒,我不確定)而不是圓。 請注意,下面使用的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類中查找。
在顯示它們時,您可以將您的分鍾和秒數四舍五入,而不是截斷它們或使用Math.Floor。 由於你有超過30秒,你的分鍾的分數值超過0.5,所以它會向上舍入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.