CLLocation 纬度是否等于零



如何检查_mapView.userLocation.location.coordinate.latitude是否等于0?

纬度是一种双typedef double CLLocationDegrees;

我尝试将其与 0、-0.000000、0.000000 和 0.00 进行比较,但没有运气。

我试过将其转换为这样的字符串

NSLog(@" lat %@", ([NSString stringWithFormat:@"%f", _mapView.userLocation.location.coordinate.latitude] == @"0.000000") ? @"yes" : @"NO" );
string  0.000000 
lat NO

但这也行不通。

我在这里错过了什么?

它存储为双精度,因此简单的数字比较就足够了:

if (coordinate.latitude == 0)

但是,如果您正在处理实际位置,则它实际上不太可能是 0,因此您可能需要先对值进行舍入。

从注释来看,您似乎假设坐标为 0,0 表示位置错误。事实并非如此。如果坐标无效,则 horizontalAccuracy 属性将为负数。这是检查无效坐标的正确方法:

if (coordinate.horizontalAccuracy < 0)

我不确定为什么,但如果我将引用存储为浮点数,它会按预期工作。

float lat = _mapView.userLocation.location.coordinate.latitude;
float lon = _mapView.userLocation.location.coordinate.longitude;  
if (lat == 0 || lon == 0) {
...
使用

swift,但你可以对 category 的 objective-c 做同样的事情:

extension CLLocation {
    func isZero() -> Bool {
        return self.coordinate.latitude == 0.0 && self.coordinate.longitude == 0.0
    }
}

你会认为上面的答案会起作用,但我最终将纬度/经度存储为浮点数,然后正常的比较按预期工作。

最新更新