Core Data and Core Location

后端 未结 3 1233
野趣味
野趣味 2020-11-30 21:04

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 loca

3条回答
  •  -上瘾入骨i
    2020-11-30 21:26

    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.

    i.e. Something like

    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];
    

    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 Also, I haven't taken into account the fact that longitude wraps!

提交回复
热议问题