I want to validate the latitude and longitude. Right now, I check just so that the value is not empty, but I want a validation to check that it is a valid latidue or longitude.
How do I do that?
My property looks like this:
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;
}
}
From MSDN
http://msdn.microsoft.com/en-us/library/aa578799.aspx
In your setter for latitude, check if the value being set falls between -90 and 90 degrees. If it doesn't, throw an exception. For your longitude, check to see if the value falls between -180 and 180 degrees. If it doesn't, throw an exception.
Use Doubles, rather than Strings. If you need to allow String input then use
Double.TryParse(string)
typically latitude/longitude are decimals, not a strings.
Alternatively you can use GeoCoordinate class that is built into .NET 4 (reference System.Device.dll). Its constructor throws on invalid longitude and latitude: