How to check that CLLocationCoordinate2D is not empty?
Asked Answered
S

4

64

How to check that CLLocationCoordinate2D is not empty?

Shornick answered 25/11, 2011 at 18:40 Comment(0)
C
145

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.

Charlot answered 27/4, 2012 at 14:59 Comment(4)
For this to work the coordinate must be initialized to kCLLocationCoordinate2DInvalid otherwise the 0,0 coordinate will be valid.Daughterinlaw
@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.Bisutun
Does this function also consider that the latitude must be a number between -90 and 90 and the longitude between -180 and 180?Anesthesiologist
@Anesthesiologist 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.Charlot
E
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")
}
Evensong answered 11/4, 2012 at 16:35 Comment(4)
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 :)Profiteer
This should be marked as correct answer, the one marked currently is misleadingIndomitability
This should be the accepted answer as the initialization to kCLLocationCoordinate2DInvalid is a must to prevent 0,0 being valid.Daughterinlaw
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).Charlot
U
9
if ( coordinate.latitude != 0 && coordinate.longitude != 0 )
{
    ....
}
Uncircumcised answered 25/11, 2011 at 18:44 Comment(4)
Haha, 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...Uncircumcised
Latitude and longitude are doubles. Never evaluate a double with an equality comparison.Alyss
There's nothing wrong with checking that it's zero, because the question asks how to check if it's empty.Uncircumcised
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?Graphitize
I
1
func isCoordinateValid(latitude: CLLocationDegrees, longitude: CLLocationDegrees) -> Bool {
    guard latitude != 0, longitude != 0, CLLocationCoordinate2DIsValid(CLLocationCoordinate2D(latitude: latitude, longitude: longitude)) else {
        return false
    }
    return true
}
Injure answered 11/12, 2018 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.