Ios 使用NSNumber类型减去纬度以查找距离
我想减去两个纬度以找到最短的距离,但如果我将-改为a+我会得到一个不同的错误“无效操作数到二进制表达式('NSNumber*'和'NSNumber*')”,我会得到这个错误,“指向接口'NSNumber'的指针上的算术,在非脆弱ABI中它不是一个常量大小”我试过使用双打和多种组合,但都不起作用Ios 使用NSNumber类型减去纬度以查找距离,ios,objective-c,nsnumber,cllocation,Ios,Objective C,Nsnumber,Cllocation,我想减去两个纬度以找到最短的距离,但如果我将-改为a+我会得到一个不同的错误“无效操作数到二进制表达式('NSNumber*'和'NSNumber*')”,我会得到这个错误,“指向接口'NSNumber'的指针上的算术,在非脆弱ABI中它不是一个常量大小”我试过使用双打和多种组合,但都不起作用 NSNumber *userLatitude = [NSNumber numberWithDouble:43.55];//sample NSArray *listOfCities = [managedO
NSNumber *userLatitude = [NSNumber numberWithDouble:43.55];//sample
NSArray *listOfCities = [managedObjectContext executeFetchRequest:request error:&error];
for (CityList *item in listOfCities){
NSLog(@"latitude is %@",item.latitude);
NSNumber *distanceLat = userLatitude - item.latitude;
然后,我将把它们和经度一起插入一个可变数组,并比较距离。使用CLLocation的一个可能解决方案是
double distance = [usersCurrentLoc distanceFromLocation:otherLoc];
其中usersCurrentLoc和otherLoc都是CLLocation变量。
我还想单独使用纬度和经度,这样我可以进行一些自定义绘图,它们也单独存储,所以我想找出正确的数据类型和最有效的解决方案
item.latitude来自核心数据,数据模型类型为double,X代码自动生成CityList类,属性为NSNumber*latitude 这是:
NSNumber *distanceLat = userLatitude - item.latitude;
需要:
NSNumber *distanceLat = @([userLatitude doubleValue] - item.latitude);
如果item.latitude
也是一个NSNumber
,那么您也需要对其调用doubleValue
NSNumber
是一个对象。你不能对这个物体进行数学运算。您需要使用doubleValue
获取其值,然后需要将结果包装到一个新的NSNumber
实例中
顺便说一句-为什么要在这里麻烦使用NSNumber
?为什么不:
double userLatitude = 43.55;
double distanceLat = userLatitude - item.latitude;
如果要减去两个NSnumber,请使用此选项
NSNumber *distanceLat = [NSNumber numberWithFloat:([userLatitude floatValue] - [item.latitude floatValue])];
CLLocationDistance meters=[newLocation distanceFromLocation:oldLocation]
上面的一个可用于查找两个位置之间的距离您是否要查找最短距离?
CLLocationCoordinate2D newCoordinate = [newLocation coordinate];
CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];