I need to compute MAX and MIN Latitude and Longitude values from a location with certain distance.
I have thousands of locations stored in CoreData, and I want to show only the ones within 5km from users location.
How can I approach this problem?
I need to compute MAX and MIN Latitude and Longitude values from a location with certain distance.
I have thousands of locations stored in CoreData, and I want to show only the ones within 5km from users location.
How can I approach this problem?
Use the CoreLocation method
distanceFromLocation:
which returns the distance (in meters) between two points as such:A suitable predicate can be constructed as:
This has the advantage that it returns the items actually within range, as opposed to the items in a square approximating the range. It's also (probably, we don't know the details) using a more accurate approximation of the range itself. It has the disadvantage that the predicate requires loading every object since it can't be expressed as an sqlite query.
Here's a possible solution:
macros to convert Degrees to Radians
macros to hold my searching distance
set the minimum and maximum Latitude, Longitude values
create predicate as follows
This will create a square around
userLocation
and check if a given location falls into its coordinates.First create a function to compute degrees to radians
Compute and create minimum and maximum
Latitude
andLongitude
valuesLast create
NSPredicate
to queryCoreData
for locations. In my case I am querying for valueslatitude
andlongitude
but you should change this to match yourCoreData
object