Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate latitude and longitude

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;      } } 
like image 733
Hysteria Avatar asked Jun 30 '11 14:06

Hysteria


People also ask

How do you validate latitude and longitude?

Each line contains a pair of co-ordinates which possibly indicate the latitude and longitude of a place. The latitude and longitude, if present will always appear in the form of (X, Y) where X and Y are decimal numbers. For a valid (latitude, longitude) pair: -90<=X<=+90 and -180<=Y<=180.

How do I find my latitude and longitude in Python?

Use the geolocator. reverse() function and supply the coordinates (latitude and longitude) to get the location data. Get the address of the location using location. raw['address'] and traverse the data to find the city, state, and country using address.


1 Answers

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.

like image 125
George Johnston Avatar answered Sep 27 '22 21:09

George Johnston