How to validate latitude and longitude
Asked Answered
R

7

78

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?

Rodenticide answered 15/10, 2011 at 22:8 Comment(0)
W
185

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

Worden answered 15/10, 2011 at 22:10 Comment(3)
An answer with an explanation, https://mcmap.net/q/263281/-validate-latitude-and-longitude. Yes, it is correct answer. However, this assumes land and sea.Palladous
Should we include border? (long >= -180 && long <= 180) or (long > -180 && long < 180) ?Orabelle
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.Maxa
H
49

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;
Hath answered 23/5, 2020 at 10:37 Comment(0)
C
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
}
Crocker answered 1/10, 2019 at 8:57 Comment(0)
S
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
}
Sharp answered 16/10, 2011 at 10:9 Comment(0)
D
0

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})?))$
Dowland answered 30/9, 2014 at 11:1 Comment(1)
I dont know what this achieves over the accepted answer (instead of complicating the matter)Decennial
G
-1
CLLocationCoordinate2D p1;
        p1.latitude  = [[punto latitud] doubleValue];
        p1.longitude = [[punto longitud] doubleValue];
        if (CLLocationCoordinate2DIsValid(p1))
        {
            [Mapa addAnnotation:annotationPoint];
        }
Gratin answered 23/7, 2014 at 17:40 Comment(1)
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.Augean
N
-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;

 }
Nightshirt answered 1/10, 2015 at 22:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.