How to check that CLLocationCoordinate2D is not empty? How to check that CLLocationCoordinate2D is not empty? ios ios

How to check that CLLocationCoordinate2D is not empty?


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.


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 = kCLLocationCoordinate2DInvalidif CLLocationCoordinate2DIsValid(myCoordinate) {    println("Coordinate valid")} else {    println("Coordinate invalid")}


if ( coordinate.latitude != 0 && coordinate.longitude != 0 ){    ....}