Ben Williams
Ben Williams

Reputation: 4695

Searching for surrounding suburbs based on latitude & longitude using Objective C

I need to identify items of interest nearby to a particular latitude/longitude. I have two sets of objects stored in Core Data:

PostalArea
- latitude
- longitude
- postcode

Store
- name 
- latitude
- longitude

I need to be able to retrieve a record from the PostalArea table, and then find stores which are closeby.

Most of the examples I've found are SQL based. I'm hoping someone can assist me with getting this working with Core Data. Ideally I would like to limit the result set to a certain number (say, 10), as opposed to limiting it based on distance.

EDIT: pulling all the records into memory is an option, if need be.

Upvotes: 2

Views: 708

Answers (2)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46728

The most common solution to this is to determine a min/max of both lat and long and then do a predicate based on those. That would narrow your search to within a circle and then with those remaining objects in memory you can sort by closest to the point.

Update

Once you have the objects in memory you can then do some fun things with them. For instance you could have a transient value called 'currentPoint' and you could then KVO on the resulting array such as:

[resultingArray setValue:aPoint forKey:@"currentPoint"];

Then you could have a method that returns the distance from that point which you can sort by.

That is one example, I am sure there are other ways but the general idea is to get a subset of locations into memory so that you can then calculate distance and finally sort.

Upvotes: 1

Charlie Martin
Charlie Martin

Reputation: 112404

How would you feel about a REST service option? Is you use the Google APIs, you can query for exactly those things. There are plenty of JSON interfaces available for Objective C.

Upvotes: 0

Related Questions