How to check that CLLocationCoordinate2D is not empty?
Asked
Active
Viewed 2.8k times
4 Answers
144
A very old topic, but I needed it now and I fixed my issue with the help of Klaas Hermanns, with a tiny change.
Instead of
if( myCoordinate == kCLLocationCoordinate2DInvalid ) {
NSLog(@"Coordinate invalid");
}
I had to use
if (CLLocationCoordinate2DIsValid(myCoordinate)) {
NSLog(@"Coordinate valid");
} else {
NSLog(@"Coordinate invalid");
}
Maybe this will help someone else :)
Edit:
As pointed out, the initialization, as covered in Klaas his post, is still necessary.

Rick van der Linde
- 2,581
- 1
- 20
- 22
-
8For this to work the coordinate must be initialized to kCLLocationCoordinate2DInvalid otherwise the 0,0 coordinate will be valid. – malhal Nov 05 '13 at 18:55
-
4@malcolmhall the (0,0) coordinate is actually a valid coordinate. Saying it is invalid might mislead some developers. However, it usually comes about as a result of some error resulting in the (0,0) coordinate. For example [anNSNumber floatValue] will return 0 if the instance is nil. But because there is nothing of interest at (0,0) most programmers are willing to mark it as invalid to identify these errors. – pnizzle Feb 13 '15 at 00:16
-
Does this function also consider that the latitude must be a number between -90 and 90 and the longitude between -180 and 180? – ViruMax Nov 19 '18 at 02:37
-
1@ViruMax from Apple: A coordinate is considered invalid if it meets at least one of the following criteria: Its latitude is greater than 90 degrees or less than -90 degrees. Its longitude is greater than 180 degrees or less than -180 degrees. – Rick van der Linde Feb 11 '19 at 11:07
60
You can use the constant kCLLocationCoordinate2DInvalid declared in CLLocation.h
Initialize your variable with
CLLocationCoordinate2D myCoordinate = kCLLocationCoordinate2DInvalid;
and later check it with:
if( myCoordinate == kCLLocationCoordinate2DInvalid ) {
NSLog(@"Coordinate invalid");
}
Addition:
Sometimes this seems to be an even better solution (as mentioned by Rick van der Linde in another answer):
if (CLLocationCoordinate2DIsValid(myCoordinate)) {
NSLog(@"Coordinate valid");
} else {
NSLog(@"Coordinate invalid");
}
Addition for Swift:
You can do the same likewise in Swift as shown here:
let myCoordinate = kCLLocationCoordinate2DInvalid
if CLLocationCoordinate2DIsValid(myCoordinate) {
println("Coordinate valid")
} else {
println("Coordinate invalid")
}

Klaas
- 22,394
- 11
- 96
- 107
-
Nice, I didn't know about this `kCLLocationCoordinate2DInvalid` constant! I end up here trying to figure out how to properly use the `CLLocationCoordinate2DIsValid` function (which does not work with the default value for `CLLocationCoordinate2D`, you have manually set it to the constant value, the way you did). And FYI, it turns out that you can use this function instead of comparing it to directly the constant value :) – Felipe Sabino Jul 22 '12 at 15:28
-
This should be marked as correct answer, the one marked currently is misleading – Lukas1 Aug 12 '13 at 12:49
-
2This should be the accepted answer as the initialization to kCLLocationCoordinate2DInvalid is a must to prevent 0,0 being valid. – malhal Nov 05 '13 at 18:55
-
Yes this should be the accepted answer (it was). But how is my answer misleading? I said that I only had to change something, meaning the initialization is still needed (I'm even referring to this answer). – Rick van der Linde Nov 06 '13 at 09:08
9
if ( coordinate.latitude != 0 && coordinate.longitude != 0 )
{
....
}

joerick
- 16,078
- 4
- 53
- 57
-
8Haha, point taken. Though they should be double comparisons, so in theory we could get as close as 2^-1074 to zero before we mistakenly forget they exist... – joerick Nov 25 '11 at 19:04
-
1Latitude and longitude are `double`s. Never evaluate a `double` with an equality comparison. – KPM Sep 06 '14 at 21:50
-
1There's nothing wrong with checking that it's zero, because the question asks how to check if it's empty. – joerick Sep 07 '14 at 11:08
-
what's funny is that this worked for me in my case, but CLLocationCoordinate2DIsValid did not. Seems that zero coordinates are legitimate for CLLocationCoordinate2DIsValid, and that when an erroneous address was geocoded, it set the coordinate to zero, and not empty? – stackOverFlew Aug 14 '16 at 19:52
1
func isCoordinateValid(latitude: CLLocationDegrees, longitude: CLLocationDegrees) -> Bool {
guard latitude != 0, longitude != 0, CLLocationCoordinate2DIsValid(CLLocationCoordinate2D(latitude: latitude, longitude: longitude)) else {
return false
}
return true
}

jawad
- 775
- 6
- 16