简体   繁体   English

使用 NSPredicate 的位置之间的距离

[英]Distance Between Locations Using NSPredicate

So I have a Core Data with an entity containing a longitude and latitude as NSNumbers .所以我有一个Core Data ,其中包含一个包含经度和纬度作为NSNumbers的实体。 I want the NSFetchRequest 's Predicate to filter based on how far the object in the database's location is from my current location.我希望NSFetchRequestPredicate根据数据库位置中的对象与我当前位置的距离进行过滤。 This would require me to convert the stored coordinates to CLLocationDegrees and then put them in a CLLocation object.这需要我将存储的坐标转换为 CLLocationDegrees,然后将它们放入CLLocation对象中。 Finally, I can do the compare with the CLLocation distanceFromLocation .最后,我可以与CLLocation distanceFromLocation进行比较。

I worded this as best as I can, does anyone have any ideas how I can do this with an NSPredicate ?我尽我所能地表达了这一点,有没有人知道我如何用NSPredicate做到这NSPredicate

Thanks!谢谢!

Try block-based predicate:尝试基于块的谓词:

CLLocationDistance maxDistance = 100.0;
CLLocation *currentLocation = ...;

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id location, NSDictionary *bindings) {
    return [location distanceFromLocation:currentLocation] <= maxDistance;
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用iPhone中的Google Map计算两个位置之间的距离 - Calculate distance between two locations using google map in iphone iOS中的位置之间的距离是否有误? - Getting wrong distance between locations in iOS? 如何在SKMaps中计算两个位置之间的距离 - How to calculate distance between two locations in SKMaps 自动更新目标c中两个位置之间的距离 - autoupdate a distance between two locations in objective c 计算NSPredicate中两个PFGeopoints之间的距离以进行解析查询 - Calculate distance between two PFGeopoints in a NSPredicate for a Parse Query 如何在目标c中以英里为单位获得两个位置之间的距离? - how to get the distance between two locations in miles in objective c? 在使用NSPredicate predicateWithBlock和在for循环中使用块之间是否存在显着的性能差异? - Is there a substantial performance difference between using NSPredicate predicateWithBlock and using a block in a for loop? NSPredicate 101:将NSPredicate与NSMutableArray自定义对象一起使用 - NSPredicate 101: Using NSPredicate with a NSMutableArray of custom objects nspredicate使用NSregularexpression - nspredicate using NSregularexpression 使用NSPredicate搜索NSArray - Search NSArray using NSPredicate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM