How to check that CLLocationCoordinate2D is not empty?
How to check that CLLocationCoordinate2D is not empty?
Asked Answered
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.
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
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")
}
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 misleading –
Indomitability
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
if ( coordinate.latitude != 0 && coordinate.longitude != 0 )
{
....
}
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
double
s. 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
func isCoordinateValid(latitude: CLLocationDegrees, longitude: CLLocationDegrees) -> Bool {
guard latitude != 0, longitude != 0, CLLocationCoordinate2DIsValid(CLLocationCoordinate2D(latitude: latitude, longitude: longitude)) else {
return false
}
return true
}
© 2022 - 2024 — McMap. All rights reserved.