我有两个坐标: @property (assign) CLLocationCoordinate2D coordinate1;@property (assign) CLLocationCoordinate2D coordinate2; 在初始化期间,我将它们设置为无效(以后能够检查它是否已设置): _coordinate1 = kCLLoc
@property (assign) CLLocationCoordinate2D coordinate1; @property (assign) CLLocationCoordinate2D coordinate2;
在初始化期间,我将它们设置为无效(以后能够检查它是否已设置):
_coordinate1 = kCLLocationCoordinate2DInvalid; _coordinate2 = kCLLocationCoordinate2DInvalid;
然后在方法中检查它们是否有效:
- (void)checkIfValid { BOOL coordinatesAreValid = (CLLocationCoordinate2DIsValid(_coordinate1) && CLLocationCoordinate2DIsValid(_coordinate2)); NSAssert(coordinatesAreValid, @"You didn't set up coordinates!"); }
在我调用方法之前,我通过这种方式设置这些坐标:
[_myclassinstance setCoordinate1:CLLocationCoordinate2DMake(mapView.region.center.longitude, mapView.region.center.latitude)]; [_myclassinstance setCoordinate2:CLLocationCoordinate2DMake(mapView.region.center.longitude, mapView.region.center.latitude)]; [_myclassinstance checkIfValid];
问题是应用声称,坐标是错误的.我打印过它们,它们似乎没问题:
(lldb) p _coordinate1 (CLLocationCoordinate2D) $1 = { latitude = -122.4612387012154 longitude = 37.72925050874177 } (lldb) p _coordinate2 (CLLocationCoordinate2D) $2 = { latitude = -122.3582012987846 longitude = 37.84612098703619 }
但是打印检查不起作用,也断言运行:
p (int)CLLocationCoordinate2DIsValid(_coordinate1) (int) $3 = 0
有谁知道为什么会这样?欢迎任何帮助:)
我不太了解CLLocationCoordinate2DIsValid应该给你什么,但根据文档: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.
因此,由于你的纬度低于-90度,我想这种方法只是在做自己的工作.
编辑:您似乎只使用了错误参数的CLLocationCoordinate2DMake.它应该是:
[_myclassinstance setCoordinate1:CLLocationCoordinate2DMake(lattitude, mapView.region.center.longitude)];
交换纬度和经度.