71

I have two UITextFields which users can enter in a latitude and longitude, these co-ordinates are then used to create a pin on an MKMapView.

I want find a way to validate whether the values they enter are actual GPS co-ordinates or just a load of rubbish. Is there any way of doing this?

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76

7 Answers7

173

The latitude must be a number between -90 and 90 and the longitude between -180 and 180.

rid
  • 61,078
  • 31
  • 152
  • 193
  • 1
    An answer with an explanation, http://stackoverflow.com/a/6536279/368691. Yes, it is correct answer. However, this assumes land and sea. – Gajus Jan 05 '14 at 22:52
  • 11
    Should we include border? (long >= -180 && long <= 180) or (long > -180 && long < 180) ? – Mehmed Oct 01 '14 at 16:43
  • 5
    Yes, include the border, but just understand for longitude, -180 and 180 are the same point. For Latitude, 90 is the geographic north pole and -90 is the geographic south pole. – Berin Loritsch Feb 19 '21 at 20:36
43

Here are the functions to validate it in JavaScript.

  1. Latitude must be a number between -90 and 90
const isLatitude = num => isFinite(num) && Math.abs(num) <= 90;
  1. Longitude must a number between -180 and 180
const isLongitude = num => isFinite(num) && Math.abs(num) <= 180;
Suman Kharel
  • 920
  • 11
  • 13
3

In Kotlin we can do something like this:

fun isValidLatLang(latitude: Double?, longitude: Double?): Boolean {
    return latitude?.toInt() in -90 until 90 && longitude?.toInt() in -180 until 180
}
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
1

Using follow latitude and longitude regular expressions, we can validate.

With escape characters in Objective-C:

Latitude RegEx:

@"^(\\+|-)?((\\d((\\.)|\\.\\d{1,6})?)|(0*?[0-8]\\d((\\.)|\\.\\d{1,6})?)|(0*?90((\\.)|\\.0{1,6})?))$"

Longitude RegEx:

@"^(\\+|-)?((\\d((\\.)|\\.\\d{1,6})?)|(0*?\\d\\d((\\.)|\\.\\d{1,6})?)|(0*?1[0-7]\\d((\\.)|\\.\\d{1,6})?)|(0*?180((\\.)|\\.0{1,6})?))$"

Normal Regular expressions for Both latitude & longitude:

Latitude RegEx:

^(\+|-)?((\d((\.)|\.\d{1,6})?)|(0*?[0-8]\d((\.)|\.\d{1,6})?)|(0*?90((\.)|\.0{1,6})?))$

Longitude RegEx:

^(\+|-)?((\d((\.)|\.\d{1,6})?)|(0*?\d\d((\.)|\.\d{1,6})?)|(0*?1[0-7]\d((\.)|\.\d{1,6})?)|(0*?180((\.)|\.0{1,6})?))$
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
abhi
  • 563
  • 6
  • 9
0

I'd do something like this

numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];  
NSNumber *latitude = [numberFormatter numberFromString:theInputString];     
if((latitude != nil) 
{
  //check it is within lat/long range
} else {
  //not even a valid number, reject it
}
Craig
  • 8,093
  • 8
  • 42
  • 74
-1
CLLocationCoordinate2D p1;
        p1.latitude  = [[punto latitud] doubleValue];
        p1.longitude = [[punto longitud] doubleValue];
        if (CLLocationCoordinate2DIsValid(p1))
        {
            [Mapa addAnnotation:annotationPoint];
        }
Matias
  • 11
  • 4
    This answer is in the Low Quality Posts review queue because it's just code with no explanation. Please improve your answer by explaining what your code does and how it answers the question. Please read [this advice on answering programming questions helpfully](http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx). – Adi Inbar Jul 23 '14 at 19:22
-1

After going through a lot of StackOverflow questions, I thought this question was asked in a simple and straightforward manner which described what I was looking for in solving my latitude/longitude validation for AMZO:A Global Map Based Station for Reporting Aliens, Monsters, Zombies and Other Interesting Events (iPhone/iPad app). Shameless, I know, but I think I deserve it for coming up with a complete and elegant answer/solution (adapting Craig's brief answer above)!

I am using the new AlertController which calls each of the following validations for latitude and longitude text inputs.

 - (BOOL) validateInput1: (NSString *) latitude
 {
     NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
     [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
     NSNumber *latitude1 = [numberFormatter numberFromString:latitude];

     if (latitude1 != nil)

     {
         //check it is within lat/long range

         if ((latitude1.floatValue > -90.0) && (latitude1.floatValue < 90.0)) {

             NSLog(@"Hello Latitude!!!");

             return 1;
         }

     } else {

         //not even a valid number, reject it

         return 0;

     }

      return 0;

 }


 - (BOOL) validateInput2: (NSString *) longitude
 {
     NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
     [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
     NSNumber *longitude1 = [numberFormatter numberFromString:longitude];

     if (longitude1 != nil)

     {
         //check it is within lat/long range

         if ((longitude1.floatValue > -180.0) && (longitude1.floatValue < 180.0)) {

             NSLog(@"Hello Longitude!!!");

             return 1;
         }

     } else {

         //not even a valid number, reject it

         return 0;

     }

     return 0;

 }
Jim Rota
  • 1,781
  • 2
  • 16
  • 21