Validate latitude and longitude
Asked Answered
B

4

39

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; 
    }
}
Beshore answered 30/6, 2011 at 14:19 Comment(4)
What do you consider valid? On the planet? On land?Dituri
Why are you taking them as strings when they're naturally numbers?Reproduce
Using string is not the best way i know that, but i can't change it now. I want to be sure it is sent as an approved coordinate and nothing else.Beshore
@Dituri - valid are values that would reflect a position on the planetTat
H
71

From 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

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.

Hellfire answered 30/6, 2011 at 14:22 Comment(4)
you can have negative lat/long. typically, the range is -90 to +90 and -180 to +180Miamiami
@Muad'Dib, that article says you can have negative numbers... (that's what the (+/-) is), it's saying from -90 to +90 for Lat, and -180 to +180 for LongFloridafloridia
Duckett He was right -- I had it wrong and changed my answer.Hellfire
Duckett isn't that what i just said? :)Miamiami
D
39

Alternatively you can use GeoCoordinate class that is built into .NET 4 (reference System.Device.dll). Its constructor throws on invalid longitude and latitude:

latitude

Type: System.Double

The latitude of the location. May range from -90.0 to 90.0.

longitude

Type: System.Double

The longitude of the location. May range from -180.0 to 180.0.

Deuteron answered 22/6, 2013 at 21:36 Comment(0)
M
26

Use Doubles, rather than Strings. If you need to allow String input then use 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;
        }
    }
Mainz answered 30/6, 2011 at 14:38 Comment(0)
G
0

typically latitude/longitude are decimals, not a strings.

Decimal degrees are an alternative to using degrees, minutes, and seconds (DMS). As with latitude and longitude, the values are bounded by ±90° and ±180° respectively. Positive latitudes are north of the equator, negative latitudes are south of the equator. Positive longitudes are east of Prime Meridian, negative longitudes are west of the Prime Meridian. Latitude and longitude are usually expressed in that sequence, latitude before longitude.

Gaskell answered 30/6, 2011 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.