核心数据和核心位置 [英] Core Data and Core Location
问题描述
我有一个具有纬度和经度属性的Core Data数据库。有没有办法使用Core Location的 getDistanceFrom:
方法来找到从GPS获得的 CLLocation
的五个最近位置?我必须使用Core Data加载所有对象,并使用 getDistanceFrom:
在每个对象上进行解析,还是有更简单的方法?
I have a Core Data database with latitude and longitude properties. Is there a way to use Core Location's getDistanceFrom:
method to find the five nearest locations to a CLLocation
obtained from the GPS? Do I have to load all the objects using Core Data and parse through them, using getDistanceFrom:
on each one or is there an easier way?
推荐答案
你必须逐个迭代它们;但我知道没有其他方法可以做到。
You will have to iterate through them one by one; as far as I know there is no other way to do it.
但是,当您从核心数据获取项目时,可以通过使用边界框使这更有效 - 这将减少将返回的对象的数量。
However, you can make this more efficient by using a bounding box when you get the items from core data - this will reduce the number of objects that will be returned.
ie像
float latMax = wantedLat + 1;
float latMin = wantedLat - 1;
float lngMax = wantedLng + 1;
float lngMin = wantedLng - 1;
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"lat > %f and lat < %f and lng > %f and lng < %f",
latMin, latMax, lngMin, lngMax];
虽然,根据你有多少数据以及间隔多近,不同于1的数字!
Though, depending on how much data you have and how closely it's spaced, you will want to use a different number than 1!
Sam
PS此外,我没有考虑经度包装!
PS Also, I haven't taken into account the fact that longitude wraps!
这篇关于核心数据和核心位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!