我想验证纬度和经度。现在,我只是检查以确保该值不为空,但我想要验证以检查它是否是有效的纬度或经度。
我该怎么做?
我的属性(property)是这样的:
public string Lat
{
get {
return this._lat;
}
set
{
base.ValidationErrors.Remove("Lat");
if (String.IsNullOrWhiteSpace(value))
{
this.ValidationErrors.Add("Lat", strings.Positions_Lat_Empty);
}
this._lat = value != null ? value.Trim() : null;
}
}
public string Lng
{
get {
return this._lng;
}
set {
base.ValidationErrors.Remove("Lng");
if (String.IsNullOrWhiteSpace(value))
{
this.ValidationErrors.Add("Lng", strings.Positions_Lng_Empty);
}
this._lng = value != null ? value.Trim() : null;
}
}
最佳答案
来自MSDN
http://msdn.microsoft.com/en-us/library/aa578799.aspx
Latitude measures how far north or south of the equator a place is located. The equator is situated at 0°, the North Pole at 90° north (or 90°, because a positive latitude implies north), and the South Pole at 90° south (or –90°). Latitude measurements range from 0° to (+/–)90°.
Longitude measures how far east or west of the prime meridian a place is located. The prime meridian runs through Greenwich, England. Longitude measurements range from 0° to (+/–)180°.
在您的纬度 setter 中,检查设置的值是否在 -90 到 90 度之间。如果没有,则抛出异常。对于您的经度,请检查该值是否在 -180 到 180 度之间。如果没有,则抛出异常。
关于c# - 验证纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6536232/