76

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?

7 Answers 7

181

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

3
  • 1
    An answer with an explanation, stackoverflow.com/a/6536279/368691. Yes, it is correct answer. However, this assumes land and sea.
    – Gajus
    Jan 5, 2014 at 22:52
  • 12
    Should we include border? (long >= -180 && long <= 180) or (long > -180 && long < 180) ?
    – Mehmed
    Oct 1, 2014 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. Feb 19, 2021 at 20:36
45

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;
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
}
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})?))$
1
  • 17
    I dont know what this achieves over the accepted answer (instead of complicating the matter) Jul 20, 2020 at 8:44
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
}
-1
CLLocationCoordinate2D p1;
        p1.latitude  = [[punto latitud] doubleValue];
        p1.longitude = [[punto longitud] doubleValue];
        if (CLLocationCoordinate2DIsValid(p1))
        {
            [Mapa addAnnotation:annotationPoint];
        }
1
-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;

 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.