简体   繁体   English

如何检查CLLocationCoordinate2D是否为空?

[英]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.

相关问题 MKPolygon - 如何确定 CLLocationCoordinate2D 是否在 CLLocationCoordinate2D 多边形中? - MKPolygon - How to determine if a CLLocationCoordinate2D is in a CLLocationCoordinate2D polygon? 如何存储CLLocationCoordinate2D? - How to store CLLocationCoordinate2D? 如何在不使用 MKMapView 的情况下检查 MKCoordinateRegion 是否包含 CLLocationCoordinate2D? - How to check if MKCoordinateRegion contains CLLocationCoordinate2D without using MKMapView? 如何检查CLLocationCoordinate2D是否位于四个CLLocationCoordinate2D Square内?在Objective C中使用Google Maps - How can I check if a CLLocationCoordinate2D is inside four CLLocationCoordinate2D Square? in Objective C with Google Maps 检查坐标矩形是否包含CLLocationCoordinate2D - Check if coordinate rectangle contains CLLocationCoordinate2D 如何获得CLLocationCoordinate2D的GMSPlace? - How to get GMSPlace for a CLLocationCoordinate2D? ios:如何使用获取CLLocationCoordinate2D <CLLocationManagerDelegate> - ios: How to obtain CLLocationCoordinate2D with <CLLocationManagerDelegate> 如何将NSValue转换为CLLocationCoordinate2D - How to Convert NSValue to CLLocationCoordinate2D 如何创建CLLocationCoordinate2d对象 - How to create a CLLocationCoordinate2d object 如何将NSMutableArray转换为CLLocationCoordinate2D - How to convert NSMutableArray to CLLocationCoordinate2D
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM