Validate latitude and longitude

£可爱£侵袭症+ 提交于 2019-12-02 17:57:16

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°.

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.

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.

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;
        }
    }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!