ios 如何检查 CLLocationCoordinate2D 不为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8273107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check that CLLocationCoordinate2D is not empty?
提问by Shmidt
How to check that CLLocationCoordinate2D is not empty?
如何检查 CLLocationCoordinate2D 不为空?
回答by Rick van der Linde
A very old topic, but I needed it now and I fixed my issue with the help of Klaas Hermanns, with a tiny change.
一个非常古老的话题,但我现在需要它,我在 Klaas Hermanns 的帮助下解决了我的问题,做了一个小小的改动。
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.
正如所指出的,Klaas 在他的帖子中提到的初始化仍然是必要的。
回答by Klaas
You can use the constant kCLLocationCoordinate2DInvalid declared in CLLocation.h
您可以使用 CLLocation.h 中声明的常量 kCLLocationCoordinate2DInvalid
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):
有时这似乎是一个更好的解决方案(正如 Rick van der Linde 在另一个答案中提到的那样):
if (CLLocationCoordinate2DIsValid(myCoordinate)) {
NSLog(@"Coordinate valid");
} else {
NSLog(@"Coordinate invalid");
}
Addition for Swift:
Swift 的补充:
You can do the same likewise in Swift as shown here:
您可以在 Swift 中执行同样的操作,如下所示:
let myCoordinate = kCLLocationCoordinate2DInvalid
if CLLocationCoordinate2DIsValid(myCoordinate) {
println("Coordinate valid")
} else {
println("Coordinate invalid")
}
回答by joerick
if ( coordinate.latitude != 0 && coordinate.longitude != 0 )
{
....
}
回答by jawad
func isCoordinateValid(latitude: CLLocationDegrees, longitude: CLLocationDegrees) -> Bool {
guard latitude != 0, longitude != 0, CLLocationCoordinate2DIsValid(CLLocationCoordinate2D(latitude: latitude, longitude: longitude)) else {
return false
}
return true
}