How to check that CLLocationCoordinate2D is not empty?

IosCllocation

Ios Problem Overview


How to check that CLLocationCoordinate2D is not empty?

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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")
}

Solution 3 - Ios

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

Solution 4 - Ios

func isCoordinateValid(latitude: CLLocationDegrees, longitude: CLLocationDegrees) -> Bool {
    guard latitude != 0, longitude != 0, CLLocationCoordinate2DIsValid(CLLocationCoordinate2D(latitude: latitude, longitude: longitude)) else {
        return false
    }
    return true
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShmidtView Question on Stackoverflow
Solution 1 - IosRick van der LindeView Answer on Stackoverflow
Solution 2 - IosKlaasView Answer on Stackoverflow
Solution 3 - IosjoerickView Answer on Stackoverflow
Solution 4 - IosjawadView Answer on Stackoverflow