[英]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.我希望
NSFetchRequest
的Predicate
根据数据库位置中的对象与我当前位置的距离进行过滤。 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.