[英]How to check that CLLocationCoordinate2D is not empty?
如何检查CLLocationCoordinate2D是否为空?
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所述的初始化仍然是必要的。
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")
}
if ( coordinate.latitude != 0 && coordinate.longitude != 0 )
{
....
}
func isCoordinateValid(latitude: CLLocationDegrees, longitude: CLLocationDegrees) -> Bool {
guard latitude != 0, longitude != 0, CLLocationCoordinate2DIsValid(CLLocationCoordinate2D(latitude: latitude, longitude: longitude)) else {
return false
}
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.