How to validate latitude and longitude
GeolocationGpsCoordinatesMkmapviewMkannotationGeolocation Problem Overview
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?
Geolocation Solutions
Solution 1 - Geolocation
Solution 2 - Geolocation
Here are the functions to validate it in JavaScript.
- Latitude must be a number between -90 and 90
const isLatitude = num => isFinite(num) && Math.abs(num) <= 90;
- Longitude must a number between -180 and 180
const isLongitude = num => isFinite(num) && Math.abs(num) <= 180;
Solution 3 - Geolocation
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})?))$
Solution 4 - Geolocation
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
}
Solution 5 - Geolocation
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
}
Solution 6 - Geolocation
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;
}
Solution 7 - Geolocation
CLLocationCoordinate2D p1;
p1.latitude = [[punto latitud] doubleValue];
p1.longitude = [[punto longitud] doubleValue];
if (CLLocationCoordinate2DIsValid(p1))
{
[Mapa addAnnotation:annotationPoint];
}