Hys*_*ria 32 c# validation coordinates
我想验证纬度和经度.现在,我只检查该值是否为空,但我想验证它是否是有效的latidue或经度.
我怎么做?
我的财产看起来像这样:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
Geo*_*ton 55
来自MSDN
http://msdn.microsoft.com/en-us/library/aa578799.aspx
纬度测量一个地方赤道北部或南部的距离.赤道位于0°,北极位于北纬90°(或90°,因为正纬度表示北极),而南极位于90°以南(或-90°).纬度测量范围从0°到(+/-)90°.
经度测量一个地方位于本初子午线的东部或西部.本初子午线贯穿英格兰格林威治.经度测量范围从0°到(+/-)180°.
在纬度设置器中,检查设置的值是否介于-90和90度之间.如果没有,抛出异常.对于经度,请检查该值是否介于-180和180度之间.如果没有,抛出异常.
Dmi*_*try 33
或者,您可以使用.NET 4中内置的GeoCoordinate类(参考System.Device.dll).它的构造函数抛出无效的经度和纬度:
纬度
键入:System.Double
位置的纬度.范围从-90.0到90.0.
经度
键入:System.Double
经度的位置.可能范围从-180.0到180.0.
Ian*_*Ian 19
使用双打,而不是字符串.如果需要允许String输入,请使用Double.TryParse(string)
public Double Lat
{
get
{
return this._lat;
}
set
{
if (value < -90 || value > 90)
{
throw new ArgumentOutOfRangeException("Latitude must be between -90 and 90 degrees inclusive.");
}
this._lat= value;
}
}
public Double Lng
{
get
{
return this._lng;
}
set
{
if (value < -180 || value > 180)
{
throw new ArgumentOutOfRangeException("Longitude must be between -180 and 180 degrees inclusive.");
}
this._lng= value;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35542 次 |
最近记录: |