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?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- back button text does not change
相关文章
- 现在使用swift开发ios应用好还是swift?
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- How do you detect key up / key down events from a
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
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!
You can't use CoreData to find the nearest object, but might want to narrow down the query by using a bounding box. To find the nearest object, you might prefer to use simply the difference in lat/long which is considerably faster than calculating the actual distances.
I had a similar question:
CoreData: Find minimum of calculated property
The following example is almost what you are looking for. It uses a SQLite function to calculate the distance in the ORDER BY clause (Haversine formula). Problem is that this example does not use Core-Data, but the SQLite API directly.
http://www.thismuchiknow.co.uk/?p=71