c# - 验证纬度和经度

标签 c# validation coordinates

我想验证纬度和经度。现在,我只是检查以确保该值不为空,但我想要验证以检查它是否是有效的纬度或经度。

我该怎么做?

我的属性(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°.

enter image description here

在您的纬度 setter 中,检查设置的值是否在 -90 到 90 度之间。如果没有,则抛出异常。对于您的经度,请检查该值是否在 -180 到 180 度之间。如果没有,则抛出异常。

关于c# - 验证纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6536232/

相关文章:

c# - 提高MMO游戏性能

Java异常处理用户输入

c++ - 通过范围变量计算点周围的积分器 x 和 y 坐标

c# - 如何安排在我的 MVC 网络应用程序中自动运行 Controller ?

c# - 在多个属性上使用 except 过滤列表

wpf - 验证按钮单击事件 wpf c#

javascript - 使用 javascript onblur 事件一次处理 1 行

c# - 为近似相似的数字生成相同的哈希码

python - 如何在python中获取由三个坐标定义的平面的倾斜角度?

java - 从 Android 应用程序获取功耗